When A Class Is Loaded As Well As Initialized Inwards Jvm - Java

Classloading too initialization inward Java
Understanding of when a shape is loaded too initialized inward JVM is 1 of the cardinal concept of Java programming language. Thanks to Java linguistic communication specification nosotros lead keep everything clearly documented too explained, but many Java programmer all the same doesn't know when a shape is loaded or when a shape is initialized inward Java. Class loading too initialization seems confusing too complex to many beginners too its truthful until having roughly sense inward belt its non e'er tardily to teach inward subtle details of How JVM industrial plant inward Java. In this Java tutorial nosotros volition encounter when shape loading occurs inward Java too when too how class too interface are initialized inward Java. I volition non teach inward exceptional of ClasLoader or How ClassLoader industrial plant in  Java, that is dependent acre of roughly other postal service I am planning. simply to perish along this article focused too concise. There are several articles on Java fundamentals inward similar How HashMap industrial plant inward Java too How Garbage collection industrial plant inward Java. If yous are interested yous tin equally good cheque those.

When Class is loaded inward Java
Class loading is done past times ClassLoaders inward Java which tin live implemented to eagerly charge a shape equally before long equally roughly other shape references it or lazy load the shape until a involve of shape initialization occurs. If Class is loaded earlier its truly beingness used it tin sit down within earlier beingness initialized. I believe this may vary from JVM to JVM. While its guaranteed past times JLS that a shape volition live loaded when at that spot is a involve of static initialization.

When a Class is initialized inward Java
After shape loading, initialization of shape takes house which way initializing all static members of class. Influenza A virus subtype H5N1 Class is initialized inward Java when :


1) an Instance of shape is created using either new() keyword or using reflection using class.forName(), which may throw ClassNotFoundException inward Java.

2) an static method of Class is invoked.
3) an static acre of Class is assigned.
4) an static acre of shape is used which is non a constant variable.
5) if Class is a exceed score shape too an assert statement lexically nested within shape is executed.

Reflection tin equally good drive initialization of class. Some methods of java.lang.reflect packet may drive shape to live initialized. JLS Strictly says that a shape should non live initialized past times whatever argue other than above.

How Class is initialized inward Java

Understanding of when a shape is loaded too initialized inward JVM is 1 of the cardinal c When a shape is loaded too initialized inward JVM - Javastatic too non static), block (static an non static), diverse classes (sub shape too super class) too diverse interfaces (sub interface, implementation shape too super interface) is initialized inward Java. Infact many Core Java interview question too SCJP query based on this concept because it impact lastly value of whatever variable if its initialized on multiple places. Here are roughly of the rules of shape initialization inward Java:

1) Classes are initialized from top to bottom too hence acre declared on exceed initialized earlier acre declared inward bottom
2) Super Class is initialized earlier Sub Class or derived shape inward Java
3) If Class initialization is triggered due to access of static field, exclusively Class which has declared static acre is initialized too it doesn't trigger initialization of super shape or sub shape fifty-fifty if static acre is referenced past times Type  of Sub Class, Sub Interface or past times implementation shape of interface.

4) interface initialization inward Java doesn't drive super interfaces to live initialized.
5) static fields are initialized during static initialization of shape spell non static fields are initialized when instance of shape is created. It way static fields are initialized earlier non static fields inward Java.

6)non static fields are initialized past times constructors inward Java. sub shape constructor implicitly telephone telephone super shape constructor earlier doing whatever initialization, which guarantees that non static or instance variables of super shape is initialized earlier sub class.

Examples of  class initialization inward Java:
Here is an illustration of when shape is initialized inward Java. In this illustration nosotros volition encounter which classes are initialized inward Java.

/**
 * Java programme to demonstrate class loading too initialization inward Java.
 */

public
class ClassInitializationTest {

    public static void main(String args[]) throws InterruptedException {
 
        NotUsed o = null; //this shape is non used, should non live initialized
        Child t = new Child(); //initializing sub class, should trigger super shape initialization
        System.out.println((Object)o == (Object)t);
    }
}

/**
 * Super shape to demonstrate that Super shape is loaded too initialized earlier Subclass.
 */

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

/**
 * Java shape which is non used inward this program, consequently non loaded past times JVM
 */

class NotUsed {
    static { System.out.println("NotUsed Class is initialized "); }
}

/**
 * Sub shape of Parent, demonstrate when precisely sub shape loading too initialization occurs.
 */

class Child extends Parent {
    static { System.out.println("static block of Sub shape is initialized inward Java "); }
    {System.out.println("non static blocks inward sub shape is initialized");}
}

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


Observation:
1) Super shape is initialized earlier sub shape inward Java.
2) Static variables or blocks are initialized earlier non static blocks or fields.
3) Not used shape is non initialized at all because its non been used, none of the cases mentioned on JLS or inward a higher house which triggers initialization of shape is non happened here.

Let's lead keep a await on roughly other illustration of shape initialization inward Java:

/**
 * Another Java programme illustration to demonstrate shape initialization too loading inward Java.
 */


public class ClassInitializationTest {

    public static void main(String args[]) throws InterruptedException {
 
       //accessing static acre of Parent through child, should exclusively initialize Parent
       System.out.println(Child.familyName);
    }
}

class Parent {
    //compile fourth dimension constant, accessing this volition non trigger shape initialization
    //protected static lastly String familyName = "Lawson";
 
    protected static String familyName = "Lawson";
 
    static { System.out.println("static block of Super shape is initialized"); }
    {System.out.println("non static blocks inward super shape is initialized");}
}

Output:
static block of Super class is initialized
Lawson


Observation
1. Here shape initialization occurs because static acre is accessed which is non a compile time constant. had yous declare "familyName" compile fourth dimension constant using final keyword inward Java (as shown inward commented section) shape initialization of super shape would non lead keep occurred.

2) Only super shape is initialized fifty-fifty though static acre is referenced using sub type.

There is roughly other example of shape initialization related to interface on JLS which explains clearly that initialization of sub interfaces does non trigger initialization of super interface. I highly recommend reading JLS 14.4 for understating  shape loading too initialization inward to a greater extent than detail.

That's all on When a shape is initialized too loaded inward Java. We lead keep seen clear guidelines shape JLS regarding shape initialization. We lead keep equally good seen the gild on which super type too sub type are initialized too gild of initialization for both static too non static fields too blocks inward Java.

Further Learning
Java Memory Management
10 Object oriented pattern principles Java programmer should know

Sumber https://javarevisited.blogspot.com/

0 Response to "When A Class Is Loaded As Well As Initialized Inwards Jvm - Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel