Different ways to create instance of an object

1. using new keyword
MyObject object = new MyObject();

2.using class.forname

3.using newInstance()
MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

4.using clone()
MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();

5. deserialization
ObjectInputStream inStream = new ObjectInputStream(anInputStream );

MyObject object = (MyObject) inStream.readObject();

No comments:

Post a Comment