loading the class and initialization of class

jayz_smith used Ask the Experts™
Hi all,

I have few doubts. Please correct me if I am wrong.
(1) " I've read so many  forums and they say   Class.forname("X") will load the class X into memory or register your class. In javadocs, they say a call to forName("X") causes the class named X to be initialized. "  in this context,  initialization of class is same as loading the class into memory ?

(2) invoking the class means creating the instance (object) of the class.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
1) Class.forname tells the Java classloader to load the class. What happens at that point really depends on the class in question. If the class has static a static block, then that would execute at that time. Usually, this is just done to "prepare" the class for a later call. A typical example is when making a database connection. You do a Class.forname("driverName") to load the class (this throws a ClassNotFoundException if there is a typo or if the class is not on your CLASSPATH). Then you do a DriverManager.getConnection(...) to actually use the loaded driver.

2) Invoking a class doesn't necessarily mean creating an instance of a class. If a class has static methods, you can invoke those methods without creating an instance of the class (e.g. Math.random() generates a random number, you don't do Math m = new Math(); m.random(); )


hi alanpetersen ,

you mean if class doesn't have any static field or any static members, that class will not get initialized.  is it true ?

Correct, if it is just a "regular" class then the bytecode just gets verified and loaded.

If it's a class you wrote, then you can be pretty sure of what it is doing. If it's someone else's class, then you have to rely on documentation.
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.


if the class is regular then class will not initialized. it will just get loaded and get executed its contents.  TRUE ?

if it's true, then what is the need of class initialization. why this word came into picture ?


if there is no any static field, then how to initialize that class ?
To follow from what Alan said:

(Note deliberate and careful use of Class and class.)

The Class.forName("X") method returns a Class object, that is, an instance of type Class. The first time an object of any type is created, a Class instance is created, it is during the creation of this that the static code blocks mentioned by Alan are executed. Also, this instance is responsible for all static methods. One and only one Class instance is constructed for a type, regardless of how many objects of that class are created.

It is the creation of this Class instance that is often referred to as initialisation of a class.

If objects of type X have already been created when forName is invoked then class X has already been initialised and the Class instance of type X that already exists will be returned.



Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial