Overview
Java allows loading classes and creating their objects dynamically at runtime.
Runtime means when the application is already running. This is useful in situations where we have the name of class as a string or the class name is also known at runtime.
Following are the two commonly used methods to load a class and create objects at runtime:
1. Class.forName()
,
2. Class.forName().getInstance()
In this article, we will take a look at these methods and the differences between these.
This method is used to load the class into Java Virtual Machine(JVM) and register it with the classloader.
It returns an object of type
java.lang.Class
that represents the loaded class.This method does not create an instance of the class; it simply loads the class into memory. Example,
Class<?> class = Class.forName("com.codippa.MyClass");
forName()
takes a String argument, which is the fully qualified name of the class.
Fully qualified name means the name of class along with its package.
forName()
throws below exceptions and errors
1. ClassNotFoundException
if the class to be loaded could not be found.
This generally happens if the jar containing this class is not present on the classpath or it is not visible.
2. LinkageError
, when the JVM attempts to link a class and its dependencies at runtime, and encounters a problem with the classpath or the dependencies themselves.
3. ExceptionInInitializerError
, when the static initialization of a class fails due to an exception being thrown in a static block or during the initialization of a static
variable.
A common use case for Class.forName()
is to dynamically load a driver while creating database connection with JDBC.
Example, below code loads MySQL JDBC driver dynamically using Class.forName()
Class.forName("com.mysql.jdbc.Driver");
Note that forName()
only loads a class. So you can get information about it such as its methods, constructors, fields or instance variables.
It does not create an object of this class. For this purpose, use newInstance()
method covered next.
Class.forName().newInstance()
We have already learnt about Class.forName()
method. It returns an object of java.lang.Class
, which has another method newInstance()
.
newInstance()
creates and returns an object of the class loaded with forName()
. Example,
MyClass obj = (MyClass) Class.forName("com.codippa.MyClass"). newInstance();
Note that you have to cast the returned value to your class type.
newInstance()
is used to create objects of classes which are not available or known at compile time.
This method may throw following exceptions:
1. IllegalAccessException
, if the class or its constructor is not accessible, they may be private.
2. InstantiationException
, if the class cannot be instantiated probably being an abstract class or interface, an array, a primitive type etc.
3. ExceptionInInitializerError
, when the static initialization of a class fails due to an exception being thrown in a static block or during the initialization of a static
variable.
4. SecurityException
, if the calling code is prohibited from calling this class.
newInstance()
is deprecated since Java 9 and you can use getDeclaredConstructor().newInstance()
method as shown below
Class clazz = Class.forName("com.codippa.MyClass"); MyClass obj = (MyClass)clazz.getDeclaredConstructor().newInstance();
Following are the differences between both these methods.
1. Purpose
Class.forName() is used to load a class into JVM while
Class.forName().newInstance()
is used to create an object of a class.
2. Exception Handling
Class.forName()
method throws 3 exceptions, out of which 1 is a checked exception, while newInstance()
method throws 4 exceptions, out of which 3 are checked exceptions.
3. Deprecation
newInstance()
method is deprecated from java 9 while forName()
is not.
Hope the article was useful.