[Javase Study Notes]-7.13 Static Memory loading
In the previous sections, we have been talking about the static keyword modified by the static keyword. How does the static in the memory be reflected when the program is running? Let's take a look at this section.
Let's take a look at an example first. We hope that through the analysis of this example, we can give beginners a deep understanding of the specific embodiment of static modification in memory.
Class Person {String name; // name, which is a member variable int age; static String country = "us"; // nationality, which is a static variable Person (String name, int age) // constructor {this. name = name; this. age = age;} public void printInfo () // non-static function {System. out. println (name + ":" + age);} public static void printCoun () // static function, print the static variable country {System. out. println (Person. country) ;}} class StaticTest {public static void main (String [] args) {Person. printCoun (); Person p = new Person ("Kobe", 37); p. printInfo ();}}Let's first look at the running results, and then analyze them step by step:
The example is very simple and the result is quite obvious. We mentioned the difference between member variables and static variables in 7.9, that is, member variables are stored in objects in the heap memory, static variables are stored in the static area of the method area.
Here, we introduce a new area of memory, that is, the method area. For methods, they will be stored in this area when the program runs.
Then we will analyze the code running process and Memory changes. Of course, before we analyze the changes, we must clarify a common sense, that isWhen we execute a class, the class will enter the memory.
The analysis process of the above Code will be clear:
1. when we run the program, the StaticTest class enters the memory, and the virtual opportunity allocates space in the non-static area of the method area to store StaticTest () {} default constructor, meanwhile, the static main (){......} The main function, of course, includes the bytecode of all the code of the main function.
2. The main function of the static zone goes into the stack memory. The main method has an object variable p.
3. Execute the "Person. printCoun () {}" method. The "Person" class enters the memory. The non-declarative area of the method area is allocated space, and the constructor Person (name, age) {…} is stored ){......} Non-static function void printInfo (){......}, In the static area of the Method Area, allocate space to store static variables country = "us" and static methods printCoun (){......}.
4. The printCoun () method of the static zone is used in the stack memory, and the static variable country is found in the static zone and printed. The console outputs the static variable "USA ".
5. The printCoun () method ends and jumps out of the method. The printCoun () method outputs the stack memory.
6. execute Person p = new Person ("Kobe", 37). In this case, create a storage object in the heap memory. If the address is 0x0056, this = 0x0056, the member variables include name and age.
7. The non-static area constructor Person (name, age) enters the stack memory, initializes the object, and initializes the object in the heap memory, name = Kobe, age = 37.
8. After Initialization is complete, assign the address 0x0056 to the object p, p = 0x0056.
9. The constructor outputs the stack memory and releases the parameter name and age.
10. Execute the p. printInfo () statement. The printInfo () method in the non-statement area is added to the stack memory. this = 0x0056.
11. Print the member variables this. name and this. age pointed to by this. The console output is Kobe: 37.
12. The execution of the printInfo () method ends. The method jumps out of the stack memory.
13. The main () function execution ends, jumps out, and the function exits the stack memory.
14. The program is running.
We have analyzed the code in the example step by step, and basically clarified the specific changes in the memory of the static keyword modified when the program is running, I hope it will be helpful in the future development process.
Finally, let's talk about a small knowledge point:The variables and methods stored in the Method Area share objects. Therefore, the method area is also called the sharing area.