Definition, a static variable in Java is also called a class variable, which begins with the creation of a class and ends with the extinction of a class. A non-static variable is called an instance variable, which begins with the creation of an instance of a class,
The demise of an instance of the bundle language class. Static variables are shared by all instances.
What about in memory? When a class is created and initialized, there are two areas, stacks, and heaps in memory. The main storage in the stack is the reference variable, the main storage in the heap
It is the real class that is instantiated. The reference variable in the stack points to the real object address in the heap. such as Aa=newa (); A This variable will be in the stack, the object of Class A that is actually new
is placed in the heap, a points to a object that is actually being new.
If there is a static variable in a class, the program will first load the static variable into memory, that is, in the heap to open a region dedicated to storage. No matter how many new classes you have,
object, the static variable is always there. In other words, a static variable will not open a new memory space for the variable once the class is initialized. And the pair of each new class
, the system will re-create a new space in the heap memory to hold the instance object of the class, and there will be a new reference variable in the stack to point to it.
Static methods are similar, but one thing to emphasize is that static methods cannot invoke non-static methods. Because the static modified method is first loaded into memory by the ClassLoader object,
At this point, other non-static methods or variables may not have been loaded.
The static is modified with the initialization of the class is generated, in the heap memory has a dedicated area to store, so only the class name point method name or variable name can be. Rather than static
The object of the class must be adjusted accordingly.
Static variables for Java