When a class is loaded and initialized in JVM - Java Example (original) (raw)

Understanding when a class is loaded and initialized in JVM is one of the fundamental concepts of Java programming language. Thanks to Java language specification we have everything clearly documented and explained, but many Java programmer still doesn't know when a class is loaded or when a class is initialized in Java. Classloading and initialization seems confusing and complex to many beginners and its true until having some experience in belt its not always easy to get into subtle details of How JVM works in Java. In this Java tutorial, we will see when class loading occurs in Java and when and how class and interface are initialized in Java.

1. When a class is loaded in Java

Classloading is done by ClassLoaders in Java which can be implemented to eagerly load a class as soon as another class references it or lazy load the class until a need for class initialization occurs.

If Class is loaded before it's actually being used it can sit inside before being initialized. I believe this may vary from JVM to JVM. While it's guaranteed by JLS that a class will be loaded when there is a need for static initialization.

2. When a class is initialized in Java

After class loading, initialization of the class takes place which means initializing all static members of the class. A Class is initialized in Java when :

  1. a static method of a class is invoked.

  2. a static field of Class is assigned.

  3. a static field of a class is used which is not a constant variable.

  4. if Class is a top-level class and an assert statement lexically nested within the class is executed.

Reflection can also cause the initialization of the class. Some methods of java.lang.reflect package may cause the class to be initialized. JLS Strictly says that a class should not be initialized for any reason other than above.

3. How Class is initialized in Java

class loading and initialization in Java - When exampleNow we know what triggers initialization of a class in Java, which is precisely documented in Java language specification. Its also important to know in which order various fields (static and non-static), block (static an non static), various classes (subclass and superclass) and various interfaces (sub interface, implementation class and super interface) is initialized in Java.

Infact many Core Java interview question and SCJP question based on this concept because it affects final value of any variable if its initialized on multiple places. Here are some of the rules of class initialization in Java:

  1. Classes are initialized from top to bottom so field declared on top initialized before field declared in bottom

  2. Super Class is initialized before Sub Class or derived class in Java

  3. If Class initialization is triggered due to access of static field, only Class which has declared static field is initialized and it doesn't trigger initialization of super class or sub class even if static field is referenced by Type of Sub Class, Sub Interface or by implementation class of interface.

  4. static fields are initialized during static initialization of class while non static fields are initialized when an instance of the class is created. It means static fields are initialized before non-static fields in Java.

  5. non-static fields are initialized by constructors in Java. sub class constructor implicitly call super class constructor before doing any initialization, which guarantees that non static or instance variables of super class is initialized before sub class.

4. Examples of class initialization in Java:

Here is an example of when class is initialized in Java. In this example we will see which classes are initialized in Java.

/**
* Java program to demonstrate class loading and initialization in Java.
*/
public class ClassInitializationTest {

public static void main(String args[]) throws InterruptedException {

NotUsed o = null; //this class is not used, should not be initialized
Child t = new Child(); //initializing sub class, should trigger super class initialization
System.out.println((Object)o == (Object)t);
}
}

/**
* Super class to demonstrate that Super class is loaded and initialized before Subclass.
*/
class Parent {
static { System.out.println("static block of Super class is initialized"); }
{System.out.println("non static blocks in super class is initialized");}
}

/**
* Java class which is not used in this program, consequently not loaded by JVM
*/
class NotUsed {
static { System.out.println("NotUsed Class is initialized "); }
}

/** * Sub class of Parent, demonstrate when exactly sub class loading and initialization occurs. */
class Child extends Parent {
static { System.out.println("static block of Sub class is initialized in Java "); }
{System.out.println("non static blocks in sub class is initialized");}
}

Output:
static block of Super class is initialized
static block of Sub class is initialized in Java
non static blocks in super class is initialized
non static blocks in sub class is initialized
false

5. Observation:

  1. Super class is initialized before sub class in Java.

  2. Not used class is not initialized at all because its not been used, none of the cases mentioned on JLS or above which triggers initialization of class is not happened here.

Let's have a look on another example of class initialization in Java:

/**
* Another Java program example to demonstrate class initialization and loading in Java.
*/

public class ClassInitializationTest {

public static void main(String args[]) throws InterruptedException {

//accessing static field of Parent through child, should only initialize Parent
System.out.println(Child.familyName);
}
}

class Parent {
//compile time constant, accessing this will not trigger class initialization
//protected static final String familyName = "Lawson";

protected static String familyName = "Lawson";

static { System.out.println("static block of Super class is initialized"); }
{System.out.println("non static blocks in super class is initialized");}
}

Output:
static block of Super class is initialized
Lawson

6. Observation Again

1. Here class initialization occurs because a static field is accessed which is not a compile time constant. had you declare "familyName" compile time constant using the final keyword in Java (as shown in commented section) class initialization of super class would not have occurred.

  1. Only super class is initialized even though the static field is referenced using sub type.

There is another example of class initialization related to the interface on JLS which explains clearly that initialization of sub interfaces does not trigger initialization of super interface. I highly recommend reading JLS 14.4 for understating class loading and initialization in more detail.

That's all When a class is initialized and loaded in Java. We have seen clear guidelines from JLS regarding class initialization. We have also seen the order on which super type and subtype are initialized and the order of initialization for both static and non-static fields and blocks in Java.

Some Javarevisited tutorials you may like: