Add to Google Reader or Homepage

How to resolve java.lang.instantiationexception

Instantiation Exception

I have discussed about this exception in detail in my new post.To have a look at it click here..

In this Post let us discuss about the java.lang.instantiation exception in detail.Let us know what is meant by this exception?why this exception is thrown? and how to solve it.

What is meant by Instantiation Exception?


There are certain classes exists for which we cannot create objects.Lets see what are those classes.

1.We all know that an abstract class cannot be instantiated.But there are some other ways available, through which we can try to instantiate a abstract class.If we use the newInstance() method for creating the object then we will not get that error at compile time.But you will get this Instantiation exception thrown.

2.An abstract class,an interface,even primitive data types like int,float,double,byte can also be represented as the Class objects.Except ordinary class other classes cannot be instantiated.

3.A class that has no default constructor i.e a constructor with no arguments.This default constructor is needed only when an argumented constructor is already defined, otherwise it is not needed.

Look at the following example to understand how to use newInstance() method

class A
{
}
class B
{
......
Class a=Class.forName("A");//representing an ordinary class
a.newInstance();
}

As you see this is another way of instantiating a class.The Class.forName() method will return the object of class Class.Using that object we can call the newInstance() method to instantiate the class A.

Similarly  for an interface like Serializable() we can do as follows

Class b=Serializable.class;
b.newInstance();

Here Serializable is an interface but i have represented it as a class using the newInstance() method.

When will be this exception is thrown?

 

When we try to instantiate the following classes using the newInstance() method, then this exception will be thrown.

1. Trying to instantiate an abstract class.

2.statements that involves instantiating an interface.

3.An array class

4.A legitimate class that has no nullary constructor.


Example situation of this exception:

instantiating an abstract class:


import java.io.*;
import java.lang.*;
import java.util.*;

abstract class Inst
{
}
class Exp
{
public static void main(String args[])throws InstantiationException,IllegalAccessException,ClassNotFoundException
{
Class cl=Class.forName("Inst");//trying to instantiate an abstract class "Inst"
cl.newInstance();
}
}
output:

Exception in thread "main" java.lang.InstantiationException
        at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance
(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at Exp.main(Exp.java:15)

 

Representing Primitive data types as class:

 

import java.io.*;
import java.lang.*;
import java.util.*;

 class Inst
{
}
class Exp
{
public static void main(String args[])throws InstantiationException,IllegalAccessException,ClassNotFoundException
{
Class cl=int.class;//representing the integer data types as a class
cl.newInstance();
}
}

output:
Exception in thread "main" java.lang.InstantiationException: int
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at Exp.main(Exp.java:13)

 

Representing an interface as a class:


import java.io.*;
import java.lang.*;
import java.util.*;

 class Inst
{
}
class Exp
{
public static void main(String args[])throws InstantiationException,IllegalAccessException,ClassNotFoundException
{
Class cl=Serializable.class;//Representing the serializable interface as a class
cl.newInstance();
}
}

Exception in thread "main" java.lang.InstantiationException: java.io.Serializabl
e
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at Exp.main(Exp.java:13)

 

Having no nullary constructor:


import java.io.*;
import java.lang.*;
import java.util.*;

 class Inst
{
int first,second;
Inst(int a,int b)    //Here there is no default constructor i.e. a constructor with no arguments
{
first=a;
second=b;
}
}
class Exp
{
public static void main(String args[])throws InstantiationException,IllegalAccessException,ClassNotFoundException
{
Class cl=Inst.class;
cl.newInstance();
}
}

output:

Exception in thread "main" java.lang.InstantiationException: Inst
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at Exp.main(Exp.java:19)

Solution:


In java, as far i know there is no way available to instantiate an abstract class or an interface or the primitive data types.So,its better to avoid these statements from the program.If this exception arises due to the absence of a default constructor,it can be overcome by  defining a no argument constructor(default constructor)




194 comments:

Post a Comment

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