InstantiationException Java

Following is the hierarchy.

java.lang.Object –> java.lang.Throwable –> java.lang.Exception –> java.lang.InstantiationException

The InstantiationException constructor is overloaded, apart default with another with string parameter.

This exception was introduced with JDK 1.0, the starting version of Java.

This exception is thrown by the JVM when it is unable to create an instance of a class when the programmer is trying to create an object of a class with newInstance() method (of class Class). It is a little bit of confusing.

First observe the following simple code where newInstance() is used. Then we will discuss the possible reasons of why this exception is thrown.

With newInstacnce() of class Class, we can create an object as you will do with new keyword.

public class Student
{
  int marks;
  public void display()
  {
    System.out.println("Hello 1");
  }
  public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException
  {

    Student std1 = new Student();
    std1.marks = 50;

    Class cls1 = std1.getClass();

    Object obj1 = cls1.newInstance();
    Student std2 = (Student) obj1;

    System.out.println("\nstd1 marks: " + std1.marks);      // prints 50
    System.out.println("std2 marks: " + std2.marks);        // prints 0
    std2.display();                                         // Hello 1 
                                              // the other way of getting Class object
    Class cls2 = Class.forName("Student");
    Object obj2 = cls2.newInstance();
    Student std3 = (Student) obj2;   
    std3.display();                                         // Hello 1
   }
}

image

The newInstance() method of java.lang.Class returns an object of class Object represented by a Class object. Let us see what I mean.

Student std1 = new Student();
Class cls1 = std1.getClass();
Object obj1 = cls1.newInstance();
Student std2 = (Student) obj1;

The getClass() method of Object class returns an object of Class, cls1. Here, the Class object cls1 represents a Student class object. The newInstance() method of java.lang.Class returns an object Object class, obj1. The obj1 represents an object of Student. If required, we can cast this obj1 to Student class object, as I did in the above code. These objects can be used to call the methods of Student.

For more on the notes of the above program and newInstance() method, refer Java newInstance.

The newInstance() method throws two checked exceptions – InstantiationException and IllegalAccessException.

The JVM may fail to create an instance for many reasons of which a few are quoted below.

1. The class being tried to create an instance may be an interface or abstract class or an array class.

2. The class may not have a default constructor but overloaded constructors exist. We know, the default constructor will not be provided by JVM when the constructor is overloaded.

For more on getClass(), newInstance() and getName() refer following links.

1. Java getClass
2. Getting Class Name in Java
3. Java newInstance
4. Reflection API uses getClass() and newInstance() method vividly.

Leave a Comment

Your email address will not be published.