Java heap, stack, method area/class variable, member variable, local variable

Source: Internet
Author: User

method area: class information, class variables (static variables and constants), methods  
heap: object, member variable  
stacks: local variables  
(1) When the program is run, the bytecode file is loaded first through the class loader, and after parsing, loading the method area! Various information about classes is stored in the method area, including class variables, class constants, and methods. For calls to the same method, different instances of the same class are called the same method that exists in the method area. The life cycle of a class variable is created from the time the program starts running and ends when the program terminates!  
(2) When a new object is in the program, the object exists in the heap, The object's variable exists in the stack, pointing to the reference in the heap! The member variables of the object are present in the heap, and when the object is recycled, the object's member variable disappears!  
(3) When the method is called, the JVM assigns a stack frame to the stack, The local variable that stores the method. When the method call ends, the local variable disappears!

Class variables: attribute information that belongs to a class, regardless of the instance of the class, where multiple instances share the same class variable, and exist in the method area. The class variable is decorated with static, which includes statics and constants. Static variables have default initial values, and constants must be declared and initialized at the same time.

Member variable: A variable belonging to an instance, only relevant to an instance, written under a class, outside of a method, not static decorated. Member variables survive as members are created and are destroyed as members are recycled.

Local variables: Declared in a method, there is no default initial value, which is created with the invocation of the method, stored in the stack, and destroyed as the end of the method call.

Java heap, stack, method area/class variable, member variable, local variable

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.