
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - InstantiationException
The InstantiationException in Java occurs when an application attempts to create an instance of a class using the newInstance()
method of the Class
class, but the class cannot be instantiated because it is either an interface or an abstract class. This is a checked exception.
Following is the reason when JVM throws an InstantiationException in Java:
- Occurs when an application attempts to create an instance of a class using the
newInstance()
method of theClass
class, but the class cannot be instantiated because it is either an interface or an abstract class.
Constructors of InstantiationException
There are two constructors of InstantiationException class:
- InstantiationException(): This constructor is used to create an InstantiationException object without any message.
- InstantiationException(String message): This constructor is used to create an InstantiationException object with a message.
Methods of InstantiationException
There are some methods of InstantiationException class:
Method | Description |
---|---|
getMessage() | It is used to return the message of the exception. |
toString() | It is used to return the detail message string of the exception. |
printStackTrace() | It is used to print the stack trace of the exception. |
Example of InstantiationException
In this example, we are trying to create an instance of an interface using the newInstance()
method of the Class
class, so JVM will throw an InstantiationException.
import java.util.ArrayList; public class InstantiationExceptionExample { public static void main(String[] args) { Class<ArrayList> c = ArrayList.class; try { ArrayList<Integer> list = c.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }
Output
Following is the output of the above code:
java.lang.InstantiationException: java.util.ArrayList at java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) at InstantiationExceptionExample.main(InstantiationExceptionExample.java:7)
Handling InstantiationException
In this example, we are trying to create an instance of an interface using the newInstance()
method of the Class
class, so JVM will throw an InstantiationException. We are handling this exception using try-catch block.
import java.util.ArrayList; public class InstantiationExceptionExample { public static void main(String[] args) { Class<ArrayList> c = ArrayList.class; try { ArrayList<Integer> list = c.newInstance(); } catch (InstantiationException e) { System.out.println("InstantiationException: " + e.getMessage()); } catch (IllegalAccessException e) { System.out.println("IllegalAccessException: " + e.getMessage()); } } }
Output
Following is the output of the above code:
InstantiationException: java.util.ArrayList
As you can see in the output, JVM throws an InstantiationException because we are trying to create an instance of an interface using the newInstance()
method of the Class
class.
Fixing the InstantiationException using reflection
Another method to fix the InstantiationException is to use the getDeclaredConstructor()
method of the Class
class to get the constructor of the class and then use the newInstance()
method of the Constructor
class to create an instance of the class.
import java.util.ArrayList; import java.lang.reflect.Constructor; public class InstantiationExceptionExample { public static void main(String[] args) { Class<ArrayList> c = ArrayList.class; try { Constructor<ArrayList> constructor = c.getDeclaredConstructor(); constructor.setAccessible(true); ArrayList<Integer> list = constructor.newInstance(); System.out.println("executed successfully"); } catch (Exception e) { e.printStackTrace(); } } }
Output
Following is the output of the above code:
executed successfully
As you can see in the output, we are creating an instance of the ArrayList
class using reflection and it is executed successfully.
How to avoid InstantiationException?
Following are the ways to avoid InstantiationException:
- Always check the class before creating an instance of it.
- Always use the
getDeclaredConstructor()
method of theClass
class to get the constructor of the class. - Always use the
newInstance()
method of theConstructor
class to create an instance of the class.
By following the above ways, you can avoid the InstantiationException in Java.