Differences between member variables (instance variables) and local variables and static variables (class variables)

Source: Internet
Author: User

Differences between member variables (instance variables) and local variables and static variables (class variables)
Differences between member variables (instance variables) and local variables:

(1) Scope
Member variable: valid for the entire class.
Local variable: Valid only within a certain range. (This generally refers to the method in the statement body)
(2) storage location
Member variable: It exists with the creation of the object and disappears with the disappearance of the object and is stored in the heap memory.
Local variables exist when the method is called or the statement is executed and stored in the stack memory.
After the method is called or the statement is completed, it is automatically released.
(3) Initial Value
Member variable: Has a default initial value.
Local variable: no default initial value. You must assign a value before use.

Differences between member variables (instance variables) and static variables (class variables:

A: Call Method
Static variables are also called class variables and can be called directly by class names. It can also be called by Object Name.
This variable belongs to the class.
Member variables are also called instance variables and can only be called by object names. This variable belongs to the object.
B: storage location
Static variables are stored in the static area of the method area.
Member variables are stored in heap memory.
C: Lifecycle
Static variables exist with the loading of classes and disappear with the disappearance of classes. Long life cycle.
Member variables exist with the creation of objects and disappear with the disappearance of objects.
D: correlation with objects
Static variables are the data shared by all objects.
Member variables are the data unique to each object.

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.