In the interview, often encounter such a written question: give you two classes of code, they are inherited relationships, each class only constructor methods and static blocks, they contain only a few simple output string to the console code, and then let us write the correct output results. This is actually an examination of our knowledge of the initialization of classes.
First, let's take a look at the following code, which is a classic way of looking at it.
- Public class Initfield {
- public static void Main (string[] args) {
- Superinitfield p = new Superinitfield ();
- Superinitfield C = new Subinitfield ();
- }
- }
- Class Superinitfield {
- Public Superinitfield () {
- System.out.println ("parent");
- }
- Static {
- System.out.println ("static parent");
- }
- }
- Class Subinitfield extends Superinitfield {
- Public Subinitfield () {
- System.out.println ("Child");
- }
- Static {
- System.out.println ("Static Child");
- }
- }
Whether or not you can write the right answer very quickly, let's put this program aside and see how the Java Virtual machine is initialized.
The JVM makes the type available to a running Java program by adding, connecting, and initializing a Java type. The life cycle of the type is as follows:
The mount and connection must be completed before initialization.
Class initialization phase, primarily assigning the correct initial value to the class variable. The "correct" initial value here refers to the starting value that the programmer expects the class variable to have. A correct initial value is given by a class variable initialization statement or static initialization statement. Initializing a class consists of two steps:
1) If the class has a direct superclass, and the direct superclass is not initialized, the direct superclass is initialized first.
2) Executes this method if the class has a class initialization method.
When will the class be initialized? The Java virtual machine specification is strictly defined for the initialization time of a class: initialized on first active use.
What are the conditions that meet the first-time active use criteria? The Java Virtual Machine specification explains this, respectively:
1) Create a new instance of the class;
2) Call the static method of the class;
3) The static field of the Operation class or interface (except the final field);
4) Invoking the specific reflection method of Java;
5) Initializes a subclass of a class;
6) Specify a class as the initialization class when the Java virtual machine starts.
In addition to the above six scenarios, all other methods are passive and do not cause initialization of the class.
Once a class is loaded, connected, and initialized, it is ready to be used. Now let's focus on the instantiation of objects, object instantiation and initialization is the activity of the beginning of the object's life.
The Java compiler generates at least one instance initialization method, the <init> () method, for each class it compiles. Each class in the source code is constructed with a corresponding <init> () method. If the class does not explicitly declare any constructor methods, the compiler generates a default parameterless constructor for the class, which simply calls the parent class's parameterless constructor.
A <init> () method may contain three of the code content: calling another <init> () method, initializing the instance variable, and constructing the code for the method body.
If the constructor is explicitly started by calling another constructor in the same class, its corresponding <init> () method body includes the following:
- A call to the <init> () method of this class;
- The byte code of the method body of the corresponding construction method is implemented.
If the constructor is not started by calling other constructors of its own class, and the object is not an object, the <init> () method includes the following:
- A call to the <init> () method of a parent class;
- The byte code of the initialization method of any instance variable;
- The byte code of the method body of the corresponding construction method is implemented.
Does the above explanation help you understand the initialization of Java types?
OK, then let's analyze the code for the beginning:
- Superinitfield p = new Superinitfield ();
- Superinitfield's Super Class is Object
- The creation of the Superinitfield object is the first active use, so the object class is initialized before the Superinitfield class variable initialization statement or static initialization statement is called, so the static parent is output
- After the class is loaded, concatenated, and initialized, an object is created, so the default constructor method of object is called first, and then its own constructor is called, so the parent is output
- Superinitfield C = new Subinitfield ();
- Subinitfield inherited from Superinitfield
- Creating the Subinitfield object, which belongs to the first active use, the parent class Superinitfield has been initialized, so whenever a Subinitfield class variable initialization statement or static initialization statement is called, the static child is output
- After the class is loaded, concatenated, and initialized, an object is created, so the constructor of the Superinitfield is called first, and then its own constructor is called, so the parent is output and then the child
By now you should generally understand the principle of Java class initialization, then I will leave it to practice, write the following code to run the results.
- Public class Test {
- Public Test () {
- System.out.println ("parent");
- }
- static{
- System.out.println ("static parent");
- }
- public static void Main (string[] args) {
- SYSTEM.OUT.PRINTLN ("main");
- }
- }
This problem is about the initialization sequence, someone has written this article, I will not say more
Initialization of Java classes and objects