Class.forName() loads the class specified by the class name string into memory without creating an instance of the class. Class.forName().newInstance() both loads the class into memory and creates an instance of the class by calling its no-argument constructor. The key difference is that Class.forName() only loads the class while Class.forName().newInstance() loads and instantiates the class. Class.forName() is commonly used to dynamically load drivers like JDBC drivers whose static initialization registers the driver with the appropriate manager.
2. public final class Class<T>extends Objectimplements Serializable,
GenericDeclaration, Type, AnnotatedElement
Instances of the class Class represent classes and interfaces in a
running Java application. An enum is a kind of class and an
annotation is a kind of interface. Every array also belongs to a class
that is reflected as a Class object that is shared by all arrays with
the same element type and number of dimensions. The primitive
Java types (boolean, byte, char, short, int, long, float, and double),
and the keyword void are also represented as Class objects.
Class has no public constructor. Instead Class objects are
constructed automatically by the Java Virtual Machine as classes
are loaded and by calls to the defineClass method in the class
loader.
2
4. Class.forName() method
This
method is used to load the class into
JVM’s memory . (without creating the object
also it will loads into the jvm’s memory.
Class.forname(“class”);
Or
Reference=Class.forName(“class”)
4
5.
By using the Class.forName() we can load any class object into JVM’s memory without creating
the object.
Ex; Class c=Class.forName(“java.lang.ArraayList”);
When the class.forName() is executed the code it will create a class boject.
Inside the class object it plays the name of the class which is loaded into JVM and the name of
the package of the class which is loaded into the JVM.
It is a factory method( means capable of constructing its own java class object is called “factory
method”.
5
6. Ex:
Class.forName("com.mysql.jdbc.Driver");
When
this statement is executed, it contains
the static block of given jdbc Driver class, so
the static block executes automatically
In this static block, there will be a logic to
create JDBC driver class object and to
register that object with driver manager
service through by calling
DriverManager.registerDriver()
6
7. Class.forName()
loads the given jdbc Driver
class bcoz of the logic available in the static
block of that jdbc driver class, the jdbc driver
class object will be registered with Driver
Manager service
7
10. Returns
the Class object associated with the
class or interface with the given string name.
Invoking this method is equivalent to:
Class.forName(className, true,
currentLoader) where currentLoader denotes
the defining class loader of the current
class.For example, the following code
fragment returns the runtime Class descriptor
10
11. Class
t = Class.forName("java.lang.Thread")
A call to forName("X") causes the class
named X to be initialized.
Parameters:className - the fully qualified
name of the desired
class.Returns:the Class object for the class
with the specified
name.Throws:LinkageError - if the linkage
failsExceptionInInitializerError - if the
initialization provoked by this method
failsClassNotFoundException - if the class
11
12. What is the diff b/w
Class.forName() and Class.forName().newInstance()?
package test;
public class Demo {
public Demo() {
System.out.println("Hi!");
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
Class clazz = Class.forName("test.Demo");
Demo demo = (Demo) clazz.newInstance();
}
}
12
13. What is the diff b/w
Class.forName() and Class.forName().newInstance()?
calling Class.forName(String) returns the Class object associated
with the class or interface with the given string name i.e. it
returns test.Demo.class which is affected to the clazz variable of
type Class.
Then, calling clazz.newInstance() creates a new instance of the
class represented by this Classobject. The class is instantiated as if
by a new expression with an empty argument list. In other words,
this is here actually equivalent to a new Demo() and returns a new
instance of Demo.
And running this Demo class thus prints the following output:
Hi!
13
14. What is the diff b/w
Class.forName() and Class.forName().newInstance()?
The big difference with the traditional new is
that newInstance allows to instantiate a class that you don't know
until runtime, making your code more dynamic.
A typical example is the JDBC API which loads, at runtime, the
exact driver required to perform the work. EJBs containers, Servlet
containers are other good examples: they use dynamic runtime
loading to load and create components they don't know anything
before the runtime.
14
15. What is the diff b/w
Class.forName() and Class.forName().newInstance()?
(...) A Driver class is loaded, and therefore automatically registered
with the DriverManager, in one of two ways:
by calling the method Class.forName. This explicitly loads the driver
class. Since it does not depend on any external setup, this way of
loading a driver is the recommended one for using
theDriverManager framework. The following code loads the
class acme.db.Driver:
Class.forName("acme.db.Driver");If acme.db.Driver has been
written so that loading it causes an instance to be created and
also calls DriverManager.registerDriver with that instance as
the parameter (as it should do), then it is in the DriverManager's list
of drivers and available for creating a connection.
(...)
15
16. What is the diff b/w
Class.forName() and Class.forName().newInstance()?
In both of these cases, it is the responsibility of the newlyloaded Driver class to register itself by
calling DriverManager.registerDriver. As mentioned, this should be
done automatically when the class is loaded.
16