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
-
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft





