Home  >  Article  >  Java  >  When is the initialization of java classes performed? (with code)

When is the initialization of java classes performed? (with code)

php是最好的语言
php是最好的语言Original
2018-08-08 10:41:491380browse

When a Java program uses a class or interface for the first time through the following 6 methods, the system will initialize the class or interface.

(1) Create an instance of the class. Ways to create an instance of a class include: using the new operator to create an instance, creating an instance through reflection, and creating an instance through deserialization.

(2) Call the class method (static method) of a certain class.

(3) Access a certain class variable or a certain class variable. Or assign a value to the variable

(4) Use reflection to force the creation of a java.lang.Class object corresponding to a certain class or interface. For example: Class,forName("Person"), if the system has not initialized the Person class, it will initialize the Person class first, and then return the java.lang.Class object corresponding to the Person class.

(5) Initialize a subclass of a certain class. All parent classes of subclasses will be initialized.

(6) Directly use the java.exe command to run a main class, and the main class is initialized first.

Note:

For a final class variable, if the value of the variable can be determined during compilation, then this class variable is equivalent to a "macro variable", and the Java compiler will At compile time, directly replace all occurrences of this class variable with its value. So even if the program uses static variables, it will not cause the class to be initialized. The following code will not print out the static initialization block

  1. public class Test12 {
    static final String STRING="宏变量";
    static{
    System.out.println("静态初始化块");
    }
    }
    class Test13{
    public static void main(String[] args) {
    System.out.println(Test12.STRING);
    }
    }

    On the contrary, if the final modified class variable cannot be determined at compile time, the value of the class variable must be determined at runtime. If its class variable is accessed through this class, it will cause the class to is initialized. (The following code will print out the static initialization block)

public class Test12 {
static final String STRING=""+System.currentTimeMillis();
static{
System.out.println("静态初始化块");
}
}
class Test13{
public static void main(String[] args) {
System.out.println(Test12.STRING);
}

Related recommendations:

Detailed explanation of Java initialization method classes and containers

Why cannot the assignment be a constant when initializing the array?

The above is the detailed content of When is the initialization of java classes performed? (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn