


The initialization and instantiation process of static variables, variables, and constructors
This article introduces the execution sequence of initialization of static variables, variables, and constructors in Java, and deepens the understanding of initialization of classes~ Generally, there are static variables, variables, constructors, and methods in a class. (In fact, the constructor can treat it as a static method to a certain extent). After combining them together, the following class is formed:
class Art{
static String ar1,ar2,ar3 ;
static{
ar1 = "good";
ar2 = "perfect";
ar3 = "beautifull";
System.out.println(ar1);
System. out.println(ar2);
System.out.println(ar3);
}
String art1,art2,art3;
{
art1 = "art1";
art2 = "art2";
art3 = "art3";
System.out.println(art1);
System.out.println(art2);
System.out.println(art3);
}
Art(){
System.out.println("Art constructor...");
}
}
As you know, in our project During the development process, we often use a major feature of Java, inheritance. So we understand Art as a base class, which will have a bunch of subclasses to inherit it, so we get another subclass Drawing
class Drawing extends Art{
static String dra1,dra2,dra3 ;
static{
dra1 = "window";
dra2 = "flay";
dra3 = "flool";
System.out.println(dra1);
System. out.println(dra2);
System.out.println(dra3);
}
String cc1,cc2,cc3;
{
cc1 = "aaa";
cc2 = "bbb";
cc3 = "ccc";
System.out.println(cc1);
System.out.println(cc2);
System.out.println(cc3);
}
Drawing(){
System.out.println("Drawing constructor...");
}
}
The Drawing class may also have subclasses , so we write another subclass of Drawing class Cartoon
public class Cartoon extends Drawing{
static String car1,car2,car3;
static{
car1 = "one";
car2 = "two";
car3 = "three";
System.out.println(car1);
System.out.println(car2); );
}
String info1,info2,info3;
{
info1 = "info1";
info2 = "info2";
info3 = "info3";
System.out.println(info1);
System.out.println(info2);
System.out.println(info3);
}
public static void main(String[] args) {
Cartoon cartoon = new Cartoon();
}
}
In the Cartoon class, we instantiate Cartoon. Let’s analyze how variables and constructors are instantiated step by step.
When we see the main function
new Cartoon(), the JVM I will look for the Cartoon class to see if there is a base class. However, I found the Drawing class based on the keyword extends.
Then I went to the Drawing class to find out whether there is the keyword extends, and then I found the Art class. class, so he looks for the Art class. In the Art class, the extends keyword is not found, so the JVM will search further according to its own rules, because if extends is not used, java will add it by default. A base class Object class, this search has been completed. At this time, the JVM will search for static variables with the static keyword
In the Object class, no static variables with the static keyword were found, so, At this time, the JVM will go down to the Art class, find the variable block with static, and put these variables into the memory block (stack), and assign them the corresponding values
static String ar1,ar2 ,ar3;
static{
ar1 = "good";
ar2 = "perfect";
ar3 = "beautifull";
System.out.println(ar1);
System.out.println(ar2);
System.out.println(ar3);
}
Then the JVM continued to go down and went to the Drawing class, and found that there Static variable block of static keyword
static String dra1,dra2,dra3;
static{
dra1 = "window";
dra2 = "flay";
dra3 = "flool";
System.out.println(dra1);
System.out.println(dra2);
System.out.println(dra3);
}
Go here At this time, the JVM does the same thing as what it does in the Art class. It allocates the variable value in the memory (stack), then executes the System.out.print() method, prints the value, and then continues. Let's go and find the Cartoon class. What we do is the same as what we did in the Drawing class.
Assign the static variable of the static keyword to the memory (stack) and print it.
static String car1,car2,car3;
static{
car1 = "one";
car2 = "two";
car3 = "three";
System .out.println(car1);
System.out.println(car2);
System.out.println(car3);
}
After the JVM completes this action, The second step is to start executing the action of instantiating the object. The JVM will start to move up from
Cartoon ——> Drawing——> Art ——> Object
First in the Object class , instantiate the variable and then execute the constructor function of object
After the execution is completed, execute along the line to the Art class, and block the variable in the Art class
String art1,art2,art3;
{
art1 = "art1";
art2 = "art2";
art3 = "art3";
System.out.println(art3);
}
Execute to the Drawing class and find that there are these variables in the Drawing class
String cc1,cc2,cc3;
{
cc1 = "aaa";
cc2 = "bbb";
cc3 = "ccc";
System.out.println(cc1);
System.out.println(cc2);
System.out.println(cc3);
}
JVM initializes these variables in the memory heap. If there is a basic type and it is found that it has not been assigned a value, it will be assigned a default value. If other types are not assigned a value, it will be assigned the default value null , and then The JVM will execute the constructor of the Drawing class. Then the JVM continues to go down and reaches the Cartoon class.
Performs the same action again
Instantiates variables
String info1,info2,info3;
{
Info1 = "info1";
Info2 = "Info2";
Info3 = "Info3";
System.out.println (Info1); ## System.out.println (Info2);
System.out.println (info3);
}
The entire program is posted below, everyone understands it first Take a look at the execution results of the program. Then execute the program again to see if your understanding is consistent with the results of the program.
static String ar1,ar2,ar3;
static{
ar1 = "good";
ar2 = "perfect";
ar3 = "beautifull";
System.out.println(ar1);
System.out .println(ar2);
System.out.println(ar3);
}
String art1,art2,art3;
{
art1 = "art1";
art2 = "art2";
art3 = "art3";
System.out.println(art1);
System.out.println(art2);
## }
Art(){
System.out.println("Art constructor...");
}
}
class Drawing extends Art{
static{
dra1 = "window";
dra2 = "flay";
dra3 = "flool";
System.out.println(dra1 );
System.out.println(dra2);
System.out.println(dra3);
}
String cc1,cc2,cc3;
{
cc1 = " aaa";
cc2 = "bbb";
cc3 = "ccc";
System.out.println(cc1);
System.out.println(cc2);
System. out.println(cc3);
}
Drawing(){
System.out.println("Drawing constructor...");
}
}
public class Cartoon extends Drawing{
static{
car1 = "one";
car2 = "two";
car3 = "three";
System.out.println(car1);
System.out.println(car2);
System.out.println(car3);
}
String info1,info2,info3;
{ (info2);
System.out.println(info3);
}
public static void main(String[] args) {
Cartoon cartoon = new Cartoon();
}
}
Post the execution results of the program below
good
perfect
beautifull
window
flay
flool
one
two
three
art1
art2
art3
Art constructor...
aaa
bbb
ccc
Drawing constructor...
info1
info2
info3
Related articles:
Complete analysis of the initialization of static static variables in Java
Related videos:
The difference between static variables and member variables-JAVA beginner video tutorial
The above is the detailed content of The initialization and instantiation process of static variables, variables, and constructors. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Analysis of memory leak phenomenon of Java programs on different architecture CPUs. This article will discuss a case where a Java program exhibits different memory behaviors on ARM and x86 architecture CPUs...

Start Spring using IntelliJIDEAUltimate version...

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

Questions and Answers about constant acquisition in Java Remote Debugging When using Java for remote debugging, many developers may encounter some difficult phenomena. It...

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...
