- 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
Different ways to create Objects in Java
Following are the different ways to create objects in Java.
Using new keyword − Mostly used method. Call any constructor with new keyword to create an object.
Tester t = new Tester();
Using Class.forName().newInstance() − Load the class using Class.forName() and then call its newInstance() method to create the object.
Tester t = Class.forName("Tester").newInstance();
Using clone() method − Get the clone object of the required object by calling its clone() method.
Tester t = new Tester(); Tester t1 = t.clone();
Using Deserialization − JVM creates a new object when it is de serialized.
Tester t = new Tester(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(t); objectOutputStream.flush(); objectInputStream = new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); Tester t1= objectInputStream.readObject();
Using Reflection − Using Constructor.newInstance() method, we can create a new object.
Constructor<Tester> constructor = Tester.class.getDeclaredConstructor(); Tester r = constructor.newInstance();
Example
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class Tester implements Serializable, Cloneable { protected Object clone() throws CloneNotSupportedException { return super.clone(); } public static void main(String args[]) throws InstantiationException, IllegalAccessException , ClassNotFoundException, CloneNotSupportedException , IOException, NoSuchMethodException, SecurityException , IllegalArgumentException, InvocationTargetException { //Scenario 1: using new keyword Tester t = new Tester(); System.out.println(t); //Scenario 2: using Class.forName().newInstance() Tester t1 = (Tester) Class.forName("Tester").newInstance(); System.out.println(t1); //Scenario 3: using clone() method Tester t3 = (Tester) t.clone(); System.out.println(t3); //Scenario 4: using Deserialization method ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(t); objectOutputStream.flush(); ObjectInputStream objectInputStream = new ObjectInputStream( new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); Tester t4 = (Tester) objectInputStream.readObject(); System.out.println(t4); //Scenario 5: using Reflection method Constructor<Tester> constructor = Tester.class.getDeclaredConstructor(); Tester t5 = constructor.newInstance(); System.out.println(t5); } }
Output
Tester@2a139a55 Tester@15db9742 Tester@6d06d69c Tester@55f96302 Tester@3d4eac69
- Related Articles
- 5 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
- 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 Add Parentheses in C++
- Create class Objects in Java using new operator
- Java Program to create Character Array from String Objects

Advertisements