Home>Article>Java> The initialization and instantiation process of static variables, variables, and constructors

The initialization and instantiation process of static variables, variables, and constructors

php是最好的语言
php是最好的语言 Original
2018-07-28 11:23:16 1371browse

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);
}

Assign a value on the heap, then execute the print statement, and then execute the constructor of the Art class. After the constructor is executed, continue Go

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);
}

Assign it to the heap memory, then print out the corresponding value, and then execute the constructor method of the Cartoon class

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.

class Art extends Object{

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 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...");
}
}

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);
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

The order of initialization execution of static members, static code blocks, ordinary members, ordinary code blocks, and constructors of Java classes Detailed explanation

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn