Friday, 28 November 2014

Different ways to create objects in Java

Different ways to create objects in Java

 

There are four different ways (I really don’t know is there a fifth way to do this) to create objects in java:

1. Using new keyword
This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.
MyObject object = new MyObject();
2. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.
MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();
 3. Using clone()
The clone() can be used to create a copy of an existing object.
MyObject anotherObject = new MyObject(); 
MyObject object = anotherObject.clone();
4. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(anInputStream ); 
MyObject object = (MyObject) inStream.readObject();
 
There are various ways:
  • Through Class.newInstance.
  • Through Constructor.newInstance.
  • Through deserialisation (uses the no-args constructor of the most derived non-serialisable base class).
  • Through Object.clone (does not call a constructor).
  • Through JNI (should call a constructor).
  • Through any other method that calls a new for you.
  • I guess you could describe class loading as creating new objects (such as interned Strings).
  • A literal array as part of the initialisation in a declaration (no constructor for arrays).
  • The array in a "varargs" (...) method call (no constructor for arrays).
  • Non-compile time constant string concatenation (happens to produce at least four objects, on a typical implementation).
  • Causing an exception to be created and thrown by the runtime. For instance throw null; or "".toCharArray()[0].
  • Oh, and boxing of primitives (unless cached), of course.
  • JDK8 should have lambdas (essentially concise anonymous inner classes), which are implicitly converted to objects.
  • For completeness (and Paŭlo Ebermann), there's some syntax with the new keyword as well
 

 

2 comments:

  1. Nice summary Saral, may be you could also add pros and cons of each approach and when to use them for benefit of fellow developers. For example new() is the preferred way but make your code tightly coupled with the implementation which makes your software inflexible. Only place where new make sense is inside factory class.

    ReplyDelete
  2. @Javin Thnks , I have added the brief explantion below..
    Each way below has different purpose. See below

    *1 Using new keyword*

    MyObject object = new MyObject();

    This is the way you create the object in java when you know which object to create.

    *2 Using Class.forName()*
    MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

    This is the way you create the object in java when you don't know which object to create in advance.I mean object that needs to be created comes from DB or property file

    *3 Using clone()*

    MyObject anotherObject = new MyObject();

    MyObject object = anotherObject.clone();

    This is the way when you need to create the copy of existing object

    *4 Using object deserialization*

    Object deserialization is nothing but creating an object from its serialized form.

    This is the way to go when you want to persist the state of object to reconstruct it later

    ReplyDelete