Add to Google Reader or Homepage

java.lang.instantiationexception-revised

Instantiation Exception:



     In Java API , it is given that,when we try to create an instance of a class "which cannot be instantiated" using the newInstance() method of the class Class,then this exception will be thrown.


what classes cannot be instantiated?

       
         
If the class object represents "the abstract class or primitive types or a class that has no nullary constructor" then those classes cannot be instantiated.

"But an other interesting fact is that this exception is not only thrown when we use the newInstance() method of the class  Class ,but also thrown when we use the new Keyword."

Let's see how?


we all know that an abstract class cannot be instantiated.But if we try to instantiate an abstract class using the new keyword then at the compile-time itself you will be shown an error message that "abstract class cannot be instantiated". Then how come this exception is thrown at run-time?

At this point i am supposed to say you about binary compatibility that exists in java programming language. Binary compatibility is nothing but whenever we create a new class(also modifying a existing class) or a package it should be  compatible with the existing classes or packages.

Here they are insisting that the new packages (or a new class) should not create inconsistency when linking to the existing packages or a class. Have a look at the following program to understand this fact.

Here i have two classes A and B as follows.

Program:



import java.util.*;
import java.io.*;
public class A
{
public static void main(String args[])
{
B b=new B();
b.call();
}
}


class B

{
public static void call()
{
System.out.println("I am inside B");
}
}



Output:


I am inside B

As you see,these two programs works fine without any error. Now what am i going to do here is change the class B as an abstract class abstract class B and then compile the program B alone.



abstract class B
{
public static void call()
{
System.out.println("I am inside B");
}
}


Since there is no statements in class B that violates the semantics of java programming language,you will not get any error.

If you execute the class A without recompiling it then this java.lang.instantiationexception  is thrown  as follows.


import java.util.*;
import java.io.*;
public class A
{
public static void main(String args[])
{
B b=new B();
b.call();
}
}


Exception in thread "main" java.lang.InstantiationError: B
        at A.main(A.java:7)


What do you infer?


From this error you may infer one thing that the binary compatibility is not preserved by this modified class B. Since the class A is not recompiled it has no idea about the new state of class B,thus causing this exception to be thrown.

It is obvious that,if we would have recompiled the class A then you will get an error message at compile time rather than getting this exception thrown at runtime.

"But there are situations,at which this exception will be thrown even though you have successfully compiled the class."


Let's consider an example situation like as follows,

We people tend to have new packages or new application included in our own project.As we download the package(eg,a jar file),most of the people (may) tend to place this jar file on the jre/lib/ext to avoid classpath settings. 

if the new version of the package arrives and if you refer to this package by including it in the classpath then "during compilation the older version would be used for resolving the references but during execution the newer version will be referenced". Thus causing this exception to be thrown.

Have  a look at the following example,I have made two different versions of the  .class file of class B  and i packed it as two different jar files.I stored one file in jre/lib/ext and the other one is referred through classpath settings. Here class A will have reference to class B


C:\>cd blog

C:\blog>cd Tool

C:\blog\Tool>javac A.java

C:\blog\Tool>java A
I am inside B



C:\blog\Tool>set CLASSPATH=".;c:/blog/Tool/ganesh/t2.jar"

C:\blog\Tool>javac A.java

C:\blog\Tool>java A
Exception in thread "main" java.lang.InstantiationError: B
        at A.main(A.java:7)



As you see,before setting the class path it works fine but when the class path had been set this exception is thrown. So when using the new packages,remove the older version of the packages and try to set the classpath to point to this new version.

I have also discussed about this exception thrown for some other reasons in my older post.To view the older post Other Common reasons for Instantiation Exception.



 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top