Classloading as well as initialization inward Java
Understanding of when a degree is loaded as well as initialized inward JVM is 1 of the cardinal concept of Java programming language. Thanks to Java linguistic communication specification nosotros receive got everything clearly documented as well as explained, but many Java programmer all the same doesn't know when a degree is loaded or when a degree is initialized inward Java. Class loading as well as initialization seems confusing as well as complex to many beginners as well as its truthful until having around sense inward belt its non e'er slowly to come inward subtle details of How JVM plant inward Java. In this Java tutorial nosotros volition run into when degree loading occurs inward Java as well as when as well as how class as well as interface are initialized inward Java. I volition non come inward especial of ClasLoader or How ClassLoader plant in Java, that is land of written report of around other post I am planning. merely to choke along this article focused as well as concise. There are several articles on Java fundamentals inward similar How HashMap plant inward Java as well as How Garbage collection plant inward Java. If you lot are interested you lot tin likewise banking concern agree those.
When Class is loaded inward Java
Class loading is done yesteryear ClassLoaders inward Java which tin live implemented to eagerly charge a degree equally shortly equally around other degree references it or lazy load the degree until a take away of degree initialization occurs. If Class is loaded earlier its genuinely beingness used it tin sit down within earlier beingness initialized. I believe this may vary from JVM to JVM. While its guaranteed yesteryear JLS that a degree volition live loaded when at that spot is a take away of static initialization.
When a Class is initialized inward Java
After degree loading, initialization of degree takes house which agency initializing all static members of class. Influenza A virus subtype H5N1 Class is initialized inward Java when :
1) an Instance of degree 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 land of Class is assigned.
4) an static land of degree is used which is non a constant variable.
5) if Class is a exceed aeroplane degree as well as an assert statement lexically nested within degree is executed.
Reflection tin likewise crusade initialization of class. Some methods of java.lang.reflect packet may crusade degree to live initialized. JLS Strictly says that a degree should non live initialized yesteryear whatever argue other than above.
How Class is initialized inward Java
static as well as non static), block (static an non static), diverse classes (sub degree as well as super class) as well as diverse interfaces (sub interface, implementation degree as well as super interface) is initialized inward Java. Infact many Core Java interview question as well as SCJP enquiry based on this concept because it touching lastly value of whatever variable if its initialized on multiple places. Here are around of the rules of degree initialization inward Java:
1) Classes are initialized from top to bottom then land declared on exceed initialized earlier land declared inward bottom
2) Super Class is initialized earlier Sub Class or derived degree inward Java
3) If Class initialization is triggered due to access of static field, solely Class which has declared static land is initialized as well as it doesn't trigger initialization of super degree or sub degree fifty-fifty if static land is referenced yesteryear Type of Sub Class, Sub Interface or yesteryear implementation degree of interface.
4) interface initialization inward Java doesn't crusade super interfaces to live initialized.
5) static fields are initialized during static initialization of degree spell non static fields are initialized when instance of degree is created. It agency static fields are initialized earlier non static fields inward Java.
6)non static fields are initialized yesteryear constructors inward Java. sub degree constructor implicitly telephone outcry upwards super degree constructor earlier doing whatever initialization, which guarantees that non static or instance variables of super degree is initialized earlier sub class.
Examples of class initialization inward Java:
Here is an illustration of when degree is initialized inward Java. In this illustration nosotros volition run into which classes are initialized inward Java.
/**
* Java computer program to demonstrate class loading as well as initialization inward Java.
*/
public class ClassInitializationTest {
public static void main(String args[]) throws InterruptedException {
NotUsed o = null; //this degree is non used, should non live initialized
Child t = new Child(); //initializing sub class, should trigger super degree initialization
System.out.println((Object)o == (Object)t);
}
}
/**
* Super degree to demonstrate that Super degree is loaded as well as initialized earlier Subclass.
*/
class Parent {
static { System.out.println("static block of Super degree is initialized"); }
{System.out.println("non static blocks inward super degree is initialized");}
}
/**
* Java degree which is non used inward this program, consequently non loaded yesteryear JVM
*/
class NotUsed {
static { System.out.println("NotUsed Class is initialized "); }
}
/**
* Sub degree of Parent, demonstrate when precisely sub degree loading as well as initialization occurs.
*/
class Child extends Parent {
static { System.out.println("static block of Sub degree is initialized inward Java "); }
{System.out.println("non static blocks inward sub degree 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
* Java computer program to demonstrate class loading as well as initialization inward Java.
*/
public class ClassInitializationTest {
public static void main(String args[]) throws InterruptedException {
NotUsed o = null; //this degree is non used, should non live initialized
Child t = new Child(); //initializing sub class, should trigger super degree initialization
System.out.println((Object)o == (Object)t);
}
}
/**
* Super degree to demonstrate that Super degree is loaded as well as initialized earlier Subclass.
*/
class Parent {
static { System.out.println("static block of Super degree is initialized"); }
{System.out.println("non static blocks inward super degree is initialized");}
}
/**
* Java degree which is non used inward this program, consequently non loaded yesteryear JVM
*/
class NotUsed {
static { System.out.println("NotUsed Class is initialized "); }
}
/**
* Sub degree of Parent, demonstrate when precisely sub degree loading as well as initialization occurs.
*/
class Child extends Parent {
static { System.out.println("static block of Sub degree is initialized inward Java "); }
{System.out.println("non static blocks inward sub degree 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 degree is initialized earlier sub degree inward Java.
2) Static variables or blocks are initialized earlier non static blocks or fields.
3) Not used degree 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 degree is non happened here.
Let's receive got a expect on around other illustration of degree initialization inward Java:
/**
* Another Java computer program illustration to demonstrate degree initialization as well as loading inward Java.
*/
public class ClassInitializationTest {
public static void main(String args[]) throws InterruptedException {
//accessing static land of Parent through child, should solely initialize Parent
System.out.println(Child.familyName);
}
}
class Parent {
//compile fourth dimension constant, accessing this volition non trigger degree initialization
//protected static lastly String familyName = "Lawson";
protected static String familyName = "Lawson";
static { System.out.println("static block of Super degree is initialized"); }
{System.out.println("non static blocks inward super degree is initialized");}
}
Output:
static block of Super class is initialized
Lawson
* Another Java computer program illustration to demonstrate degree initialization as well as loading inward Java.
*/
public class ClassInitializationTest {
public static void main(String args[]) throws InterruptedException {
//accessing static land of Parent through child, should solely initialize Parent
System.out.println(Child.familyName);
}
}
class Parent {
//compile fourth dimension constant, accessing this volition non trigger degree initialization
//protected static lastly String familyName = "Lawson";
protected static String familyName = "Lawson";
static { System.out.println("static block of Super degree is initialized"); }
{System.out.println("non static blocks inward super degree is initialized");}
}
Output:
static block of Super class is initialized
Lawson
Observation
1. Here degree initialization occurs because static land is accessed which is non a compile time constant. had you lot declare "familyName" compile fourth dimension constant using final keyword inward Java (as shown inward commented section) degree initialization of super degree would non receive got occurred.
2) Only super degree is initialized fifty-fifty though static land is referenced using sub type.
There is around other example of degree 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 degree loading as well as initialization inward to a greater extent than detail.
That's all on When a degree is initialized as well as loaded inward Java. We receive got seen clear guidelines cast JLS regarding degree initialization. We receive got likewise seen the gild on which super type as well as sub type are initialized as well as gild of initialization for both static as well as non static fields as well as blocks inward Java.
Further Learning
Java Memory Management
10 Object oriented blueprint principles Java programmer should know