On the difference between static variable, member variable and local variable _java

Source: Internet
Author: User

The difference between a static variable and a member variable:

A: Belong to different

Static variables: Belong to class, class variable

Member variables: objects, object variables, instance variables

B: Different memory locations

Static variables: Static area of method area

Member variables: Heap memory

C: Different life cycles

Static variables: Static variables are loaded as classes are loaded, disappearing as classes disappear

Member variables: member variables exist as objects are created and disappear as objects disappear

D: Calling different

Static variables: Can be invoked by object name, or by class name

Member variables: Only called by object name

The difference between a member variable and a local variable:

A: The position in the class is different

Member variables: Outside of methods in a class

Local variables: In the method definition or the method declaration

B: In-memory locations are different

Member variables: in heap memory

Local variables: in stack memory

C: Different life cycles

Member variables: exist as objects are created and disappear as objects disappear

Local variable: exists as a method call and disappears as the method is called

D: Different initialization values

Member variable: has default initialization value

Local variables: There is no default initialization value, you must define, assign, and then use.

Precautions :

Local variable names can be the same as member variable names, when used in a method, using the proximity principle.

The above is a small series for everyone to talk about static variables, member variables, local variables, the difference of all the contents of the three, I hope to help you, a lot of support cloud Habitat Community ~

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.