Return Java Basics: Six trigger class loading timing

Foreword

Under what circumstances would trigger class loading it be? This will combine the code demo to talk about several situations, we want to help.

Class loading timing

When to start the first phase of class loading process: the load? Java Virtual Machine specification does not perform the obligation, to this point can be given to specific virtual machine to free grasp. But for the initialization phase, the virtual machine specification the strict requirements of the following situations must initialize the class immediately, if the class is not been initialized, you need to trigger its initialization.

Create an instance of the class

In order to verify the class loader, we configure a JVM argument

-XX:+TraceClassLoading 监控类的加载
复制代码

In IDE configuration is as follows:

demo code:

public class ClassLoadInstance {

    static {
        System.out.println("ClassLoadInstance类初始化时就会被执行!");
    }

    public ClassLoadInstance() {
      System.out.println("ClassLoadInstance构造函数!");
    }
}

public class ClassLoadTest {

    public static void main(String[] args) {
        ClassLoadInstance instance = new ClassLoadInstance();
    }
}

复制代码

operation result:

in conclusion:

When new ClassLoadInstance instance, found ClassLoadInstance is loaded, so to create a new instance of an object, the class loader will be triggered.

Access class static variable

demo code:

public class ClassLoadStaticVariable {

    static {
        System.out.println("ClassLoadStaticVariable类初始化时就会被执行!");
    }

    public static int i = 100;

    public ClassLoadStaticVariable() {
      System.out.println("ClassLoadStaticVariable构造函数!");
    }

}

public class ClassLoadTest {

    public static void main(String[] args) {
        System.out.println(ClassLoadStaticVariable.i);
    }
}
复制代码

operation result:

in conclusion:

When accessing class ClassLoadStaticVariable static variable i, it was found ClassLoadStaticVariable class is loaded, so static variables access the class will trigger class loading.

note:

When accessing final modification of static variables will not trigger class loading, because at compile time this has been constants in the constant pool.

Access class static method

demo code:

public class ClassLoadStaticMethod {

    static {
        System.out.println("ClassLoadStaticMethod类初始化时就会被执行!");
    }

    public static void method(){
        System.out.println("静态方法被调用");
    }

    public ClassLoadStaticMethod() {
      System.out.println("ClassLoadStaticMethod构造函数!");
    }

}

public class ClassLoadTest {

    public static void main(String[] args) {
        ClassLoadStaticMethod.method();
    }
}
复制代码

operation result:

in conclusion:

When the static method to access class ClassLoadStaticMethod the method, it was found ClassLoadStaticMethod class is loaded, so static method to access class will trigger class loading.

reflection

demo code:

package classload;

public class ClassLoadStaticReflect {

    static {
        System.out.println("ClassLoadStaticReflect类初始化时就会被执行!");
    }

    public static void method(){
        System.out.println("静态方法被调用");
    }

    public ClassLoadStaticReflect() {
      System.out.println("ClassLoadStaticReflect构造函数!");
    }

}

public class ClassLoadTest {

    public static void main(String[] args) throws ClassNotFoundException {
        Class.forName("classload.ClassLoadStaticReflect");
    }
}
复制代码

operation result:

in conclusion:

When the reflection type ClassLoadStaticReflect obtained, it was found ClassLoadStaticReflect class is loaded, the reflection will trigger class loading.

When initializing a class, the class has not yet found his father initialization, the first trigger initialization of the parent class

demo code:

//父类
public class ClassLoadSuper {
    static {
        System.out.println("ClassLoadSuper类初始化时就会被执行!这是父类");
    }

    public static int superNum = 100;

    public ClassLoadSuper() {
        System.out.println("父类ClassLoadSuper构造函数!");
    }
}
//子类
public class ClassLoadSub extends ClassLoadSuper {

    static {
        System.out.println("ClassLoadSub类初始化时就会被执行!这是子类");
    }

    public static int subNum = 100;

    public ClassLoadSub() {
        System.out.println("子类ClassLoadSub构造函数!");
    }

}

public class ClassLoadTest {

    public static void main(String[] args) throws ClassNotFoundException {
        ClassLoadSub classLoadSub = new ClassLoadSub();
    }
}
复制代码

operation result:

Read operating results, is not found online that road classic face questions ( instantiated order to talk about class ?) It is also very clear. Fathers static class variable / static block of code -> another subclass of static variables / static block of code -> parent class constructor -> subclass constructor

in conclusion:

Examples of subclasses ClassLoadSub, found the parent class ClassLoadSuper first loaded, so when initializing a class, the class has not yet found his father initialization, the first trigger initialization of the parent class

When the virtual machine starts, the definition of the main () method to initialize the class

demo code:

package classload;

public class ClassLoadTest {

    public static void main(String[] args)  {
       System.out.println(ClassLoadSub.subNum);
    }
}
复制代码

operation result:

in conclusion:

When the virtual machine is powered on, even if there ClassLoadSub, ClassLoadSuper, ClassLoadTest and other class is loaded, but the first to be loaded ClassLoadTest that defines the main () method of the class will first trigger class loading.

Exercises and Summary

Trigger class loading six timing analysis done for us all, it is not to be a problem not think something more to say it? Next, we analyze the class loader a classic face questions now.

class SingleTon {  
    private static SingleTon singleTon = new SingleTon();  
    public static int count1;  
    public static int count2 = 0;  
  
    private SingleTon() {  
        count1++;  
        count2++;  
    }  
  
    public static SingleTon getInstance() {  
        return singleTon;  
    }  
}  
  
public class ClassLoadTest {  
    public static void main(String[] args) {  
        SingleTon singleTon = SingleTon.getInstance();  
        System.out.println("count1=" + singleTon.count1);  
        System.out.println("count2=" + singleTon.count2);  
    }  
}  

复制代码

operation result:

analysis:

  1. SingleTon.getInstance (), call the static method, triggering SingleTon class loading.
  2. SingleTon class loading initialization, in order to initialize static variables.
  3. Performed first private static SingleTon singleTon = new SingleTon ();, after calling the constructor, count1, count2 are 1;
  4. Executed in the order public static int count1; no value, it is still count1 1;
  5. Sequentially performing public static int count2 = 0; count2 therefore becomes zero.

Personal Public Number

Welcome to the concern, we will study together, discuss together.

Guess you like

Origin juejin.im/post/5d872bdfe51d4561eb0b275b