About NewTechnoBuzz
Contact Us

Wednesday, July 16, 2014

What is the use of class “java.lang.Class", Class.forName() and Class.newInstance() method?

java.lang.Class is a built-in class that represent instances of all data types used in a Java application in the JVM. Every time JVM creates an object, it also creates a java.lang.Class object that describes the type of the object. Instances of classes, interfaces, enumerations, annotations, arrays, primitives and void are all represented Class objects. All instances of the same class share the same Class object and you can obtain the Class object by calling the getClass() method of the object. This method is inherited from java.lang.Object.

The java.lang.Class class offers a set of methods to help to determine if the given Class object represents a specific group of data type:
  • boolean isAnnotation() - Returns true if this Class object represents an annotation type.
  • boolean isArray() - Determines if this Class object represents an array class.
  • boolean isEnum() - Returns true if and only if this class was declared as an Enum in the source code.
  • boolean isInterface() - Determines if the specified Class object represents an interface type.
  • boolean isPrimitive() - Determines if the specified Class object represents a primitive type.
Suppose you create two instances of class called Employee e.g.

Employee A = new Employee();
Employee B = new Employee();

if(A.getClass() == B.getClass()) {
   System.out.println("A and B are instances of same class");
   System.out.println("A and B are instances of different class");

In this case it will print "A and B are instances of same class" because they are both instance of Employee class.


It gives you the class object, which is useful for reflection. The methods that this object has are defined by Java, not by the programmer writing the class. It returns the Class-Type for the given name.


Class employee = Class.forName("com.gsms.common.dao.entity.Employee");
System.out.println("Number of public methods: " + employee.getMethods().length);

By using above code, we can find out the total number methods in Employee class.


It creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. In other words, this is here actually equivalent to a new Employee() and returns a new instance of Employee.

But, the big difference with the traditional new is that newInstance allows to instantiate a class that you don't know until runtime, making your code more dynamic.


Employee employee = (Employee) Class.forName("test.MyClass").newInstance();
System.out.println("String representation of Employee instance: " + employee.toString());

We need Class.forName() and Class.newInstance() because many times it happens that we don’t know the name of the class to instantiate while writing code , we may get it from config files ,database , network or from any upstream. This way of creating object which provides a platform for many frameworks e.g. Spring ,Struts which uses java reflection.

Please feel free to post your comments if you have any question. Also, if you find any error then please let me know.