Monday, 4 May 2015

Different Ways to Create an Object in Java

There are at least four ways to create a new object:

1) Using new

Person obj = new Person();

2) Using clone

Person p = new Person();
Person obj = (Person)p.clone();

3) Using Class.forName(...)

Person obj2 = (Person) Class.forName("Person").newInstance();

4) De-serializing

 InputStream instream = new FileInputStream("Person.ser");
    ObjectInputStream in = new ObjectInputStream(instream);
    Person object = (Person) in.readObject();
although technically, a de-serialized object is an old object that’s been reloaded - possibly...

If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...

No comments:

Post a Comment