Tuesday, 3 January 2017

What are different ways to Create Objects In Java ?


There are various ways to create Objects in Java, Some ways are described below with examples.

1 ) using new keyword
WaysToCreateObject object_1 = new WaysToCreateObject();

2 ) using Class.forName()
WaysToCreateObject object_2 = (WaysToCreateObject)Class.forName("java_oops_programs.WaysToCreateObject").newInstance();

3 ) using clone() method of Object class
WaysToCreateObject object_3 = (WaysToCreateObject)object_1.clone();

4 ) using class.getClassLoader().loadClass()
WaysToCreateObject object_4 = (WaysToCreateObject)WaysToCreateObject.class.getClassLoader().loadClass("java_oops_programs.WaysToCreateObject").newInstance();

5 ) using constructor of java.lang.Reflect
Constructor<?> cons = WaysToCreateObject.class.getDeclaredConstructors()[0];
WaysToCreateObject object_5 = (WaysToCreateObject) cons.newInstance();

Example :

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class WaysToCreateObject implements Cloneable {
public void show(){
System.out.println("Show Method");
}
public static void main(String[] args)  {
//First Way to Create Object : using new 
WaysToCreateObject object_1 = new WaysToCreateObject();
object_1.show();
//Second Way to Create Object : using Class.forName()
try {
WaysToCreateObject object_2 = (WaysToCreateObject)Class.forName("java_oops_programs.WaysToCreateObject").newInstance();
object_2.show();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
//Third Way to Create Object : using clone()
try {
WaysToCreateObject object_3 = (WaysToCreateObject)object_1.clone();
object_3.show();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
//Fourth Way to Create Object : using class.getClassLoader().loadClass()
try {
WaysToCreateObject object_4 = (WaysToCreateObject)WaysToCreateObject.class.getClassLoader().loadClass("java_oops_programs.WaysToCreateObject").newInstance();
object_4.show();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
//Fifth Way to Create Object : using constructor of java.lang.Reflect;
try {
Constructor<?> cons = WaysToCreateObject.class.getDeclaredConstructors()[0];
WaysToCreateObject object_5 = (WaysToCreateObject) cons.newInstance();
object_5.show();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
Output :

Show Method
Show Method
Show Method
Show Method
Show Method

      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment