In Java, there are two ways to create new instance of class viz the new operator and the newInstance method. Both of them will create a new instance of a class. Both will first load the class first in to memory and then the construction process will start using constructor of class. If you are looking to create object without constructor then you need to look at serialization process. There are some difference between new operator and newInstance method related to working and usage as listed below:

  • new operator can be used with any constructor of the class by passing any number of arguments as accepted by the constructor.
    newInstance method requires the presence of no-arg constructor in the class for which it has been invoked. If you want to use a constructor with newInstance, then you need to get an instance of Constructor class for any constructor and then invoke newInstance like:

    Class class = Class.forName("Foo");     
    Constructor const = class.getConstructor(Foo.class);  
    Foo foo = null;   
    const.newInstance(foo);   
    
  • Using new operator doesn’t require explicit class loading as it is internally handled by the JVM.
    For newInstance() method an instance of that class’s Class object is required (Class.forName(“Foo”).newInstance(); or as shown in above point). The Class object referring to the underlying class is obtained by invoking the forName method.
  • The use of new operator is recommended when the name of class is known at compile time.
    Since newInstance uses reflection to create an object of class, it is recommended to be used when the class is not known at compile time but is determined at run time.
  • Since there is no extra processing related to method invocation like forName in new operator, it is faster than newInstance.
    The use of newInstance results in extra processing on part of JVM (type checks, security checks) and hence is not recommended to be used for performance degradation reasons.(at least when thousands of instances are being created using newInstance)
  • All Java developers are supposed to know the new operator as it is basic concept which is taught at beginner level, so there is nothing special to learn about it.
    Not all developers working on a application be aware of reflection and hence there is a learning curve for beginners working on the code with newInstance method.
  • You can see new operator being used in any normal Java program.
    newInstance is being used at multiple places inside Java especially in server side like loading and instantiating servlets, applets, JNDI stub/skeletons, JDBC database drivers.
  • With new operator, the class loading and object creation is done by the default class loader of JDK.
    But with newInstance method, one can explicitly specify the class loader to be used for loading class and object instantiation.
  • There are very less chances for runtime exception with new operator. Only rare case is when the class was present during compile time but was not available on classpath during runtime.
    The use of newInstance method with Class.forName(String …) can result in runtime exception even if the class name passed as argument to forName method is invalid.
  • The use of new operator results in generation of corresponding byte code in the .class file.
    When newInstance is used, there is no extra byte code generated for object creation inside the class as object creation is handled dynamically.
  • With new operator there is inherent type checking and compiler error is shown if the class doesn’t exist.
    Since the class name is passed as argument to Class.forName method as string, there is no compile type checking and usually results in run time exception as described in one of the earlier points.

Related Posts

Difference between new operator and Class.forName().newInstance() admin Core Java
In Java, there are two ways to create new instance of class viz the new operator and the newInstance method. Both of them will create a new instance of a class. Both will first load the class first in to memory and then the construction process will start using...
In Java, there are two ways to create new instance of class viz the new operator and <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#newInstance%28%29">the newInstance method</a>. Both of them will create a new instance of a class. Both will first load the class first in to memory and then the construction process will start using constructor of class. If you are looking to create object without constructor then you need to look at <a href="http://www.javaexperience.com/object-serialization-in-java/">serialization process</a>. There are some difference between new operator and newInstance method related to working and usage as listed below: <ul><li>new operator can be used with any constructor of the class by passing any number of arguments as accepted by the constructor. newInstance method requires the presence of no-arg constructor in the class for which it has been invoked. If you want to use a constructor with newInstance, then you need to get an instance of Constructor class for any constructor and then invoke newInstance like: 1</li> </ul> <ul> <li>Using new operator doesn't require explicit class loading as it is internally handled by the JVM. For newInstance() method an instance of that class's Class object is required (Class.forName("Foo").newInstance(); or as shown in above point). The Class object referring to the underlying class is obtained by invoking the forName method.</li> </ul> <ul> <li>The use of new operator is recommended when the name of class is known at compile time. Since newInstance uses reflection to create an object of class, it is recommended to be used when the class is not known at compile time but is determined at run time.</li> </ul> <ul> <li>Since there is no extra processing related to method invocation like forName in new operator, it is faster than newInstance. The use of newInstance results in extra processing on part of JVM (type checks, security checks) and hence is not recommended to be used for performance degradation reasons.(at least when thousands of instances are being created using newInstance)</li> </ul> <ul> <li>All Java developers are supposed to know the new operator as it is basic concept which is taught at beginner level, so there is nothing special to learn about it. Not all developers working on a application be aware of reflection and hence there is a learning curve for beginners working on the code with newInstance method.</li> </ul> <ul> <li>You can see new operator being used in any normal Java program. newInstance is being used at multiple places inside Java especially in server side like loading and instantiating servlets, applets, JNDI stub/skeletons, JDBC database drivers.</li> </ul> <ul> <li>With new operator, the class loading and object creation is done by the default class loader of JDK. But with newInstance method, one can explicitly specify the class loader to be used for loading class and object instantiation.</li> </ul> <ul> <li>There are very less chances for runtime exception with new operator. Only rare case is when the class was present during compile time but was not available on classpath during runtime. The use of newInstance method with Class.forName(String ...) can result in runtime exception even if the class name passed as argument to forName method is invalid.</li> </ul> <ul> <li>The use of new operator results in generation of corresponding byte code in the .class file. When newInstance is used, there is no extra byte code generated for object creation inside the class as object creation is handled dynamically.</li> </ul> <ul> <li>With new operator there is inherent type checking and compiler error is shown if the class doesn't exist. Since the class name is passed as argument to <a href="http://media.techtarget.com/tss/static/articles/content/dm_classForname/DynLoad.pdf">Class.forName method</a> as string, there is no compile type checking and usually results in run time exception as described in one of the earlier points.</li> </ul>
How to use Verbose options in Java
Use Case for Binary, Octal and HexaDecimal in Java
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Latest posts by admin (see all)

Comments

comments