Static keyword memory analysis and static keyword Analysis

Source: Internet
Author: User

Static keyword memory analysis and static keyword Analysis

Generally, Java divides memory into stack memory, heap memory, and method areas. stack memory is used to store some basic types of variables and arrays (arrays are also a reference type) and object reference variables, and the heap memory is mainly used to place objects, that is, the new objects in the program. Static: static variables and Methods Modified with static, these variables and methods are actually assigned a "location" in the memory (this location is also called static zone, method zone, data zone, and shared zone ). Since the location is specified in the memory, their "size" seems to be fixed, with the location and size features, it is very convenient to open up space in stacks or stacks.For static things, when JVM loads a class, it opens up the space (memory space) of these static variables in the memory ), that is, space is allocated to instances of these member variables during compilation.

Next let's look at a piece of code

1 package demo; 2 3 public class StaticDemo {4 public static void main (String [] args) {5 Visitor visitor1 = new Visitor (); 6 System. out. println ("count:" + visitor1.count); // 1 7 System. out. println ("visitCount:" + Visitor. visitCount); // 1 8 9 Visitor visitor2 = new Visitor (); 10 System. out. println ("count:" + visitor2.count); // 111 System. out. println ("visitCount:" + Visitor. visitCount); // 212 13 Visitor visitor3 = new Visitor (); 14 visitor3.count = 5; 15 visitor3.visitCount = 0; // return to the 016 System. out. println ("count:" + visitor1.count); // 117 System. out. println ("count:" + visitor2.count); // 118 System. out. println ("count:" + visitor3.count); // 519 System. out. println ("visitCount:" + visitor1.visitCount); // 020 System. out. println ("visitCount:" + visitor2.visitCount); // 021 System. out. println ("visitCount:" + visitor3.visitCount); // 022} 23} 24 25 class Visitor {26 int count; 27 static int visitCount; 28 29 public Visitor () {30 count ++; 31 visitCount ++; 32} 33}

From the code above, with the static modifier variable, the instance of each class changes to the same copy, that is, there is only one static modifier variable in the memory, without common member variables, each instance has its own copy. We use graphs to analyze the code of the StaticDemo class, first, the JVM compiled the Visit class and StaticDemo class and then loaded them to the method area. In the Visit method area, the JVM checks that there is a static modified variable visitCount, A memory (called static zone) is opened for storage.

Then the JVM will automatically find the main method and open up a space for it in the stack, and then look at the code line 5th. At this time, a visitor visitor1 came, and JVM opened a memory for visitor1 in the stack, it also points to the Visitor memory space in the heap. Because count and visitCount are member variables, the default values are 0. According to the running results of lines 6th and 7th of the Code, we can get the value of the count and visitCount variables in the heap memory.

Similarly, rows 9, 10, and 11 are similar to rows 5, 6, and 7. In this case, the memory condition and the value of the variable count and visitCount are similar.

Next, a visitor3 is created, and both count and visitCount are assigned a new value.

Visitor1, visitor2, and visitor3 have their respective count variables in the heap space. When visitor3 changes the count variable value, the count variable values of visitor1 and visitor2 are not affected, when visitor3 heap visitCount is assigned a new value, because visitor1, visitor2, and visitor3 both have 0x0001 in the static zone, visitor1, visitor2, the visitCount value accessed by visitor3 is the latest 0. It can be seen that static variables are irrelevant to specific instances and belong to the entire class. In programming, when all objects share a data, we can define this member variable as static, the visitCount shown above.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.