Static variables for Java

Source: Internet
Author: User

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

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.