In this blog post we will talk about How many ways we can create an object in Java? We see there are 6 different ways to create an Object in Java.
Method-1
We can create an Object using new keyword. This is the most common way to create an object in java and almost 99% of objects are created in this way.
Example: MyClass object1 = new MyClass();
Method-2
We can dynamically load Class using Class.forName(“Qualified Name of Class”) in refection after that we can make use of newIntance() method to create an Object of provided “Qualified Name of Class”)
Loading class dynamically
Class cls = Class.forName(“kkjavatutorials.com.MyClass”);
MyClass object2 = (MyClass)cls. .newInstance();
Above two lines, we can write in Single line as below
MyClass object2 = (MyClass) Class.forName(“kkjavatutorials.com.MyClass”).newInstance();
Method-3
We can use Object’s class clone() method to create a copy of an existing object.
MyClass secondObject = new MyClass();
MyClass object3 = (MyClass) secondObject.clone();
Method-4
We can create an Object using ClassLoader as below
Object object4 = MyClass.class.getClassLoader().loadClass(“kkjavatutorials.com.MyClass”).newInstance();
Method-5
We can use the Constructor class from the java.lang.reflect
Class<MyClass> class1 = MyClass.class;
Constructor<?> constructor = class1.getDeclaredConstructors()[0];
MyClass object5 = (MyClass) constructor.newInstance();
Method-6
We can create Object using Deserialization process. Deserialization process is nothing but creating an object from its serialized form.
Now Let’s create an Example where we will see all the above 6 methods to create an object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
package kkjavatutorials.com; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.lang.reflect.Constructor; /** * @author KK JavaTutorials * There are 6 different ways to create an Object in Java */ public class MyClass implements Cloneable, Serializable { private static final long serialVersionUID = 1L; public MyClass() { System.out.println("MyClass Object is created"); } @Override protected Object clone() throws CloneNotSupportedException { return (MyClass) super.clone(); } @SuppressWarnings("unused") public static void main(String[] args) throws Exception { //1.Create an Object using new keyword MyClass object1 = new MyClass(); //2.Create an Object using reflection MyClass object2 = (MyClass) Class.forName("kkjavatutorials.com.MyClass").newInstance(); //3.Create an Object using clone MyClass myClass = new MyClass(); MyClass object3 = (MyClass) myClass.clone(); //4.Create an Object using ClassLoader Object object4 = MyClass.class.getClassLoader().loadClass("kkjavatutorials.com.MyClass").newInstance(); //5.Use the Constructor class from the java.lang.reflect Class<MyClass> class1 = MyClass.class; Constructor<?> constructor = class1.getDeclaredConstructors()[0]; MyClass object5 = (MyClass) constructor.newInstance(); /*6.Create Object using Serialization process * First write an Object in a file using Serialization and after that Create object using Deserialization*/ ObjectOutputStream objectOutputStream = null; try { FileOutputStream fileOutputStream = new FileOutputStream("kkjava.txt"); objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(object4); //write object to file objectOutputStream.flush(); } catch (Exception e) { e.printStackTrace(); }finally{ if(objectOutputStream != null) objectOutputStream.close(); } ObjectInputStream objectInputStream = null; try { objectInputStream = new ObjectInputStream(new FileInputStream("kkjava.txt")); MyClass object6 = (MyClass) objectInputStream.readObject(); System.out.println(object6.toString()); } catch (Exception e) { e.printStackTrace(); }finally{ if(objectInputStream != null) objectInputStream.close(); } } } |
Sample output
1 2 3 4 5 6 |
MyClass Object is created MyClass Object is created MyClass Object is created MyClass Object is created MyClass Object is created kkjavatutorials.com.MyClass@33909752 |
That’s all about How many ways we can create an object in Java?
You May Also Like:
Why should I choose Java for Software Development? What are Pros and Cons of Java?
Can we cast an int value into byte type variable? what if the value of int is larger than byte?
Can We store a double value in a long type variable without casting?
Why Java is not 100% Object-oriented language?
What are the differences between 32-bit and 64-bit versions of Java?
Can we call static method with null object?
Can we override static method in Java?
What will be the output of following java program?
What is the difference Between java.util.Date and java.sql.Date in Java ?
What is difference between using Serializable & Externalizable Interfaces in Java?
Which Java data type would you choose for storing sensitive information, like passwords, and Why?
What is left shift << and Unsigned right shift >>> operator in Java? How are these useful?
If you have any feedback or suggestion please feel free to drop in below comment box.