Tuesday 17 November 2015

What are the ways to create objects in java

There are 5 ways to create objects in Java
    1. Using New keyword
    2. Using New Instance (Reflection) or Class.forName()
    3. Using Clone
    4. Using ClassLoader
    5. Using Deserilization   
1. MyObject object1 = new MyObject();
2. MyObject object2 = (MyObject) Class.forName                                        ("com.getsure.MyObject").newInstance();
3. MyObject anotherObject = new MyObject();
    MyObject object3 = anotherObject.clone();
4. Object object4 = MyObject.class.getClassLoader().loadClass("getsure.com.MyObject").newInstance();
5. ObjectInputStream ois = new ObjectInputStream(anInputStream );
       MyObject object5 = (MyObject) ois.readObject();  
 
   
 

No comments:

Post a Comment