- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
5 different ways to create objects in Java
Consider a class Tester which has implemented Cloneable interface. Now you can initialize an object using following five ways:
1. Using new keyword.
Tester tester1 = new Tester();
2. Using Class.forName() method
Tester tester2 = (Tester)Class.forName("Tester").newInstance();
3. Using clone method.
Tester tester3 = tester1.clone();
4. Using Constructor.forName() method
Tester tester4 = Tester.class.getConstructor().newInstance();
5. Using Deserialization
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream ); Tester tester5 = (MyObject) objectInputStream.readObject();
Using new keyword is the most preferred one.
- Related Articles
- Different ways to create Objects in Java
- Different ways to create an object in java?
- Different ways to concatenate Strings in Java
- Different ways to overload a method in Java
- Different ways to print exception messages in Java
- Different ways to traverse an Array in Java?
- Different ways for Integer to String conversion in Java
- Extending a list in Python (5 different ways)
- Different ways to format long with Java System.out.format
- Create Objects of a Class in Java
- How to create wrapper objects in JShell in Java 9?
- What are the different ways to print an exception message in java?
- What are the different ways to iterate over an array in Java?
- Different Ways to Generate String by using Characters and Numbers in Java
- Create class Objects in Java using new operator

Advertisements