This article is shared from Huawei cloud community< [java] static variables and static code blocks >Author: Dajin (Inner Mongolia).
Today's topic:
Did your static variables and static code blocks execute today?
Don't say much. Let's start today's topic.
Let's introduce a common sense:
The initialization of static member attribute is earlier than that of static code block;
Static code block refers to the initialization of a class, which is earlier than the creation of an object;
Class static fields are initialized only once.
Topic 1: output what?
class Father{ public static int m = 33; static{ System.out.println("The parent class is initialized"); } } class Child extends Father{ static{ System.out.println("Subclass initialized"); } } class StaticTest{ public static void main(String[] args){ System.out.println(Child.m); System.out.println(new Child()); } }
answer:
The parent class is initialized
33
Subclass initialized
Child1@2781e022
Topic 2: output what?
class Const{ public static final String NAME = "I'm a constant"; static{ System.out.println("initialization Const class"); } } public class FinalTest{ public static void main(String[] args){ System.out.println(Const.NAME); } }
Answer: I am a constant
detailed description:
The variable of static final is in the preparation stage of class loading (not yet initialized):
Allocate memory for static variables of the class and assign them to default values
For this stage, the following points need to be noted:
- Only static variables modified by static are allocated memory and assigned default values (such as 0, 0L, null, false, etc.).
- The static literal constant of static final is directly assigned an initial value (the initial value is not the default value).
Topic 3: output what?
class Const{ static{ System.out.println("initialization Const class"); } } public class ArrayTest{ public static void main(String[] args){ Const[] con = new Const[5]; } }
Output: null
Topic 4: output what?
class Other { public static Other o1 = new Other(); public static Other o2 = new Other(); { System.out.println("Tectonic block"); } static { System.out.println("Static block"); } public static void main(String[] args) { Other other = new Other(); } }
answer:
Tectonic block
Tectonic block
Static block
Tectonic block
Is brain melon seed buzzing!!!
Let's introduce the class loading process and help you debug.
Class loading is divided into the following steps:
The whole life cycle includes seven stages: loading, verification, preparation, parsing, initialization, use and unloading.
- Loading: binary The class file loads all kinds of files, and the class loader loads them
- Verification: verify the correctness of classes, such as format and method rewriting
- Preparation: initialization assignment of static field. This is a default value.
- Resolution: symbol references (programming principles) are resolved to direct references.
- Initialization: assignment in the code of static field.
The initialization process is the static domain loading process in common sense.
The following four conditions trigger.
- When new, getstatic, putstatic and invokestatic bytecode instructions are encountered, if the class has not been initialized, its initialization needs to be triggered first. The most common Java code scenarios for generating these four instructions are: when instantiating an object using the new keyword, when reading or setting the static field (static) of a class (static modified and final modified, except for the static field that has put the result into the constant pool during compilation), and when calling the static method of a class.
- Using Java When the method of lang.reference package makes reflection calls to a class, if the class has not been initialized, its initialization needs to be triggered first.
- When initializing a class, if it is found that its parent class has not been initialized, the initialization of its parent class needs to be triggered first.
- When the virtual machine starts, the user needs to specify a main class to be executed, and the virtual opportunity executes the main class first.
In addition to the above four cases, there are several special cases. Class initialization is passively loaded.
- When a static field in the parent class is referenced through a subclass, the reference to the subclass is a passive reference. Therefore, the subclass will not be initialized, only the parent class will be initialized.
- A constant is stored in the constant pool of the class calling it in the compilation stage. In essence, it is not directly referenced to the class that defines the constant, so the initialization of the class that defines the constant will not be triggered. This actually completes the "preparation" phase.
- Referencing a class through an array definition does not trigger class initialization.
Read the title again:
Therefore, combined with common sense:
The initialization of static member variables is earlier than that of static code blocks;
Static code block refers to the initialization of a class, which is earlier than the creation of an object;
Class static fields are initialized only once.
Look at the above code again. Can you get the answer?
Click focus to learn about Huawei cloud's new technologies for the first time~