When A Shape Is Loaded Together With Initialized Inward Jvm - Java

When A Shape Is Loaded Together With Initialized Inward Jvm - Java - Hallo sahabat BEST LEARNING JAVA, Pada Artikel yang anda baca kali ini dengan judul When A Shape Is Loaded Together With Initialized Inward Jvm - Java, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel core java, Artikel core java interview question, Artikel JVM Internals, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : When A Shape Is Loaded Together With Initialized Inward Jvm - Java
link : When A Shape Is Loaded Together With Initialized Inward Jvm - Java

Baca juga


When A Shape Is Loaded Together With Initialized Inward Jvm - Java

Classloading in addition to initialization inwards Java
Understanding of when a shape is loaded in addition to initialized inwards JVM is 1 of the key concept of Java programming language. Thanks to Java linguistic communication specification nosotros receive got everything clearly documented in addition to explained, but many Java programmer nevertheless doesn't know when a shape is loaded or when a shape is initialized inwards Java. Class loading in addition to initialization seems confusing in addition to complex to many beginners in addition to its truthful until having about sense inwards belt its non ever tardily to come inwards subtle details of How JVM industrial plant inwards Java. In this Java tutorial nosotros volition come across when shape loading occurs inwards Java in addition to when in addition to how class in addition to interface are initialized inwards Java. I volition non come inwards item of ClasLoader or How ClassLoader industrial plant in  Java, that is acre of report of about other postal service I am planning. merely to continue this article focused in addition to concise. There are several articles on Java fundamentals inwards similar How HashMap industrial plant inwards Java in addition to How Garbage collection industrial plant inwards Java. If yous are interested yous tin laissez passer on the axe besides banking concern tally those.

When Class is loaded inwards Java
Class loading is done past times ClassLoaders inwards Java which tin laissez passer on the axe hold out implemented to eagerly charge a shape equally presently equally about other shape references it or lazy load the shape until a bespeak of shape initialization occurs. If Class is loaded earlier its genuinely beingness used it tin laissez passer on the axe 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 hold out loaded when in that location is a bespeak of static initialization.

When a Class is initialized inwards Java
After shape loading, initialization of shape takes house which agency initializing all static members of class. H5N1 Class is initialized inwards Java when :


1) an Instance of shape is created using either new() keyword or using reflection using class.forName(), which may throw ClassNotFoundException inwards 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 transcend degree shape in addition to an assert statement lexically nested within shape is executed.

Reflection tin laissez passer on the axe besides crusade initialization of class. Some methods of java.lang.reflect packet may crusade shape to hold out initialized. JLS Strictly says that a shape should non hold out initialized past times whatsoever argue other than above.

How Class is initialized inwards Java

Understanding of when a shape is loaded in addition to initialized inwards JVM is 1 of the key c When a shape is loaded in addition to initialized inwards JVM - Javastatic in addition to non static), block (static an non static), diverse classes (sub shape in addition to super class) in addition to diverse interfaces (sub interface, implementation shape in addition to super interface) is initialized inwards Java. Infact many Core Java interview question in addition to SCJP inquiry based on this concept because it touching on in conclusion value of whatsoever variable if its initialized on multiple places. Here are about of the rules of shape initialization inwards Java:

1) Classes are initialized from top to bottom in addition to therefore acre declared on transcend initialized earlier acre declared inwards bottom
2) Super Class is initialized earlier Sub Class or derived shape inwards Java
3) If Class initialization is triggered due to access of static field, solely Class which has declared static acre is initialized in addition to 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 inwards Java doesn't crusade super interfaces to hold out initialized.
5) static fields are initialized during static initialization of shape patch non static fields are initialized when instance of shape is created. It agency static fields are initialized earlier non static fields inwards Java.

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

Examples of  class initialization inwards Java:
Here is an representative of when shape is initialized inwards Java. In this representative nosotros volition come across which classes are initialized inwards Java.

/**
 * Java computer program to demonstrate class loading in addition to initialization inwards Java.
 */

public
class ClassInitializationTest {

    public static void main(String args[]) throws InterruptedException {
 
        NotUsed o = null; //this shape is non used, should non hold out 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 in addition to initialized earlier Subclass.
 */

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

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

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

/**
 * Sub shape of Parent, demonstrate when just sub shape loading in addition to initialization occurs.
 */

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

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


Observation:
1) Super shape is initialized earlier sub shape inwards 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 inwards a higher house which triggers initialization of shape is non happened here.

Let's receive got a await on about other representative of shape initialization inwards Java:

/**
 * Another Java computer program representative to demonstrate shape initialization in addition to loading inwards Java.
 */


public class ClassInitializationTest {

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

class Parent {
    //compile fourth dimension constant, accessing this volition non trigger shape initialization
    //protected static in conclusion 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 inwards 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 inwards Java (as shown inwards commented section) shape initialization of super shape would non receive got occurred.

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

There is about 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 in addition to initialization inwards to a greater extent than detail.

That's all on When a shape is initialized in addition to loaded inwards Java. We receive got seen clear guidelines shape JLS regarding shape initialization. We receive got besides seen the fellowship on which super type in addition to sub type are initialized in addition to fellowship of initialization for both static in addition to non static fields in addition to blocks inwards Java.

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


Demikianlah Artikel When A Shape Is Loaded Together With Initialized Inward Jvm - Java

Sekianlah artikel When A Shape Is Loaded Together With Initialized Inward Jvm - Java kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel When A Shape Is Loaded Together With Initialized Inward Jvm - Java dengan alamat link https://bestlearningjava.blogspot.com/2019/09/when-shape-is-loaded-together-with.html

Belum ada Komentar untuk "When A Shape Is Loaded Together With Initialized Inward Jvm - Java"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel