The difference between a member variable and a local variable

Source: Internet
Author: User

The difference between a member variable and a local variable has a few points:

1. Differences in Location:

A member variable is a variable within a class that is defined outside the method.

A local variable is a variable that is defined within a method.

2. Difference in function:

The function of a member variable is used to describe a property of a thing.

The function of a local variable is to provide a variable to use inside the method.

3. Differences in life cycle:

The life cycle of a member variable is created as the object is created and disappears as the object disappears.

When a local variable is called to the corresponding method, it is executed when the local variable statement is created, and the local variable disappears immediately after its own scope.

4. Differences in initialization:

The member variable has a default initial value. The exact initial value depends on the data type of the variable.

int 0

Double 0.0

Float 0.0f

Char '

Boolean false

String NULL

A local variable has no default initial value. To initialize before it can be used.

The difference between a member variable and a local variable

Related Article

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.