Talking about the instantiation steps of classes in Java

  • 2021-06-29 10:58:45
  • OfStack

Briefly talk about static on some personal points.

For java engineers, static is very easy to be asked during an interview.

To put it right, static is static in writing.I actually think of it as "global".What is global?Global properties, global methods, global code blocks.

Global attributes, global methods, and a better understanding is that all objects of this class have common attributes and methods.Because the entire class is common, it can be called directly by declaration.I understand it as the properties and methods of the Singleton Pattern.The so-called singleton pattern means that all objects declared by this class share these properties and methods.An object modifies this property, and all objects call this property again with the modified value.

The following is mainly about 1 code block modified by static. One of the main points for the interview is when the static code block was loaded.

First we need to understand the java virtual machine loading process for classes.First, JVM converts the class's byte code into the corresponding Class object through classloader.classloader is a parent-delegated method for finding class files.

It delegates the parent class's classloader lookup first (the parent class also has a parent and so on, until it no longer inherits another classloader's class), and if it can't find it, it finds it by itself.Throw an exception if you can't find it yourself.That's preparation. When this class is used a few times, we'll show the fossilization class (initialize its parent first if there is one), and we'll handle static-modified code during initialization.That's the static code block we're going to talk about here.Each class notification is initialized once. (We can control whether a class is initialized by changing the second parameter in the method Class.forName().

Below is a classic example found on the Internet.

The interview topics are as follows: Please write out the results after the program is executed.


package extend; 
public class X { 
Y y=new Y(); 
static{ 
 System.out.println("tttt"); 
} 
X(){ 
 System.out.println("X"); 
} 
public static void main(String[] args) { 
  new Z(); 
} 
} 
class Y{ 
Y(){ 
 System.out.println("Y"); 
} 
} 
class Z extends X{ 
Y y=new Y(); 
static{ 
 System.out.println("tt"); 
} 
Z(){ 
 System.out.println("Z"); 
} 
}

Don't tell us the final result first, let's analyze it.Step 1 and Step 1 to launch the results.

1. First analyze the results of the execution of a program, we have to find the entry to the program before we can start the analysis.This is the main() method. 2. We found that the main () method in the X class, to execute the main () method, the X class has to be loaded into memory first. 3. What will you do when the X class is loaded?Don't worry. Let's see what static does first. Don't know.Tell you: static is executed when the class is first loaded and will not execute in the future. 4. Knowing the role of static, then the X class is loaded, then the static attributes and static statement blocks (static) of the X class are executed first, and the execution sequence is to see who executes first.Execute only at this time, not later. 5. So one output is tttt, no problem. 6.When the static statement block of the X class is finished, it is time to execute the main() method. 7. new Z();This method is executed. 8. Since new Z();The Z class is then loaded.Because the Z class inherits the X class.Therefore, the X class must be loaded first.Because the X class has already been loaded.So there is no need to load the X class at this time.static statement block to execute Z class when Z class is loaded 9. Then you'll print out tt. 10. Instantiate the object when you've added it all. 11. You must instantiate X before instantiating Z, right.Because the construction method of the subclass calls the construction method of the parent class. 12. Instantiate the X class first. 13. The pair must be initialized before the X method can be executed.That is to get all the attributes.Then the property Y of the X class is obtained. 14. Y y=new Y() of X class;To be executed.That is, Y will be printed. 15. Next, execute System.out.println ("X"); 16. Then the Z construction method is executed 17. Similarly, the attribute Y y=new Y() of Z is obtained first.Print Y. 18. Execute System.out.println ("Z");

That's the whole process.Now you know the result:

tttt
tt
Y
X
Y
Z

Keep in mind that interviews may mark a few sentences that allow you to choose the order in which they will be executed.But as long as you know the principles, it's not difficult.

summary


Related articles: