Friday, July 30, 2021

When a class is loaded and initialized in JVM - Java Example

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.




Classloading and initialization in Java

I will not go into detail about ClasLoader or How ClassLoader works in  Java, which is the subject of another post I am planning. just to keep this article focused and concise. There are several articles on Java fundamentals in Javarevisited like How HashMap works in Java and How Garbage collection works in Java. If you are interested you can also check those.


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) an instance of the class is created using either a new() keyword or using reflection using class.forName(), which may throw ClassNotFoundException in Java.

2) a static method of a class is invoked.
3) a static field of Class is assigned.
4) a static field of a class is used which is not a constant variable.
5) 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) interface initialization in Java doesn't cause super interfaces to be initialized.
5) 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.

6) 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) Static variables or blocks are initialized before non static blocks or fields.
3) 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.

2) 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:

8 comments :

Anonymous said...

Very nice blogs and very well understood the system.

michee said...

good article, waiting for the ClassLoader one!:)
ps: you're missing a child class in the last example.

javin paul said...

Thanks michee, Actually child class is same in both class loading example. only modification I did on Parent class on second example is addition of static field "familyName". Regarding ClassLoader one,its little complex topic especially if you want to cover J2EE environment, which is I guess the place where ClassLoader magic lies :), hope to complete it soon. I have recently updated by post on 3 ways to solve NoClassDefFoundError in Java with some ClassLoader bits, let me know how do you find it.

michee said...

ok, i'll check it now, thanks!

Anonymous said...

Very well explained, Javin. But I would like to ask "While accessing compile time constant, if class initialization is not triggered, then when and how that constant gets initialized ?"

Thanks...

SARAL SAXENA said...

In Java, class variables are initialised in the following order:

Static variables of your superclasses
All static variables of this class are set to their default values.
Static variables, and static initialisation blocks, in declaration order.
Instance variables of your superclasses
All instance variables of this class are set to their default values.
Instance variables, and instance level initialisation blocks, in declaration order
1 & 2 are only done the very first time that a class is instantiated.


So, given the following code:

class Test
extends TestSuper
{
final int ti1;
final int ti2 = counter ++;
{ ti1 = counter ++; }
static final int ts1;
static final int ts2 = counter ++;
static { ts1 = counter ++; }

public static void main(String[] argv) {
Test test1 = new Test();
printTest(test1);
Test test2 = new Test();
printTest(test2);
}
private static void printTest(Test test) {
System.out.print("ss2 = " + test.ss2);
System.out.print(", ss1 = " + test.ss1);
System.out.print(", ts2 = " + test.ts2);
System.out.println(", ts1 = " + test.ts1);
System.out.print("si2 = " + test.si2);
System.out.print(", si1 = " + test.si1);
System.out.print(", ti2 = " + test.ti2);
System.out.println(", ti1 = " + test.ti1);
System.out.println("counter = " + test.counter);
}
}

class TestSuper
{
static int counter = 0;
final int si1;
final int si2 = counter ++;
{ si1 = counter ++; }
static final int ss1;
static final int ss2 = counter ++;
static { ss1 = counter ++; }
}

Then we get the following output:

ss2 = 0, ss1 = 1, ts2 = 2, ts1 = 3
si2 = 4, si1 = 5, ti2 = 6, ti1 = 7
counter = 8
ss2 = 0, ss1 = 1, ts2 = 2, ts1 = 3
si2 = 8, si1 = 9, ti2 = 10, ti1 = 11
counter = 12


From this output we can see that the fields are initialised in the order specified in the list.

Now, as to the second question, can re-ordering the fields change the class behaviour. Yes, by re-ordering the fields you change the initialisation order of the fields. Now, in the specific case where all of the fields are independent, this won't affect the observed behaviour, however whenever the fields are not independent, for example in the above code, then re-ordering the fields could change their initialised values.

For example, if the three lines:

static final int ss1;
static final int ss2 = counter ++;
static { ss1 = counter ++; }
are changed to:

static final int ss1;
static { ss1 = counter ++; }
static final int ss2 = counter ++;
Then the output would change to:

ss2 = 1, ss1 = 0, ts2 = 2, ts1 = 3
si2 = 4, si1 = 5, ti2 = 6, ti1 = 7
counter = 8
That is, ss2, and ss1 would change values.

The reason for this is that this behaviour is specified in the Java Language Specification.

Unknown said...

Hello!

I guess expression "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" is not true as it says. Because all super classes will be initialized, but not subclasses.

public static void main(String[] args) {
System.out.println(Child.hello);
}

private class Child extends Parent{
static String hello = "Hello";
static { System.out.println("Child"); }
}
private class Parent extends GrandParent{
static { System.out.println("Parent"); }
}
private class GrandParent {
static { System.out.println("GrandParent"); }
}

Output:
GrandParent
Parent
Child
Hello

Natraj said...

For your point : 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.

Readers pls consider below example to help your understanding:

http://stackoverflow.com/questions/9898097/what-are-the-rules-dictating-the-inheritance-of-static-variables-in-java

Thanks,
Natraj

Post a Comment