Comparison of member variables, local variables and static variables

Source: Internet
Author: User

1: Differences between member variables and local variables (understanding)
(1) Different positions in the class
Member variables: Outside Methods in class
Local variables: In a method definition or on a method declaration
(2) different locations in memory
Member variables: in the heap
Local variables: in the stack
(3) Different life cycle
Member variables: As objects are created, they disappear as the object disappears
Local variables: As the method is called, it disappears as the method's call is complete
(4) different initialization values
Member variables: with default values
Local variables: No default values, must be defined, assigned, and then used

The difference between a static variable and a member variable
A: Belong to different
Static variable: Belongs to class, class variable
Member variables: belong to object, object variable, instance variable
B: Different memory locations
Static variables: Static area of the method area
Member Variable: heap memory
C: Different life cycle
Static variables: Static variables are loaded as the class is loaded and disappear as the class disappears
Member variables: Member variables are present as the object is created and disappear as the object disappears
D: Call Different
Static variables: Can be called by object name, or by class name
Member variable: can only be called by object name

Comparison of member variables, local variables and static variables

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.