Home  >  Article  >  Java  >  The difference between member variables and local variables

The difference between member variables and local variables

巴扎黑
巴扎黑Original
2017-06-26 11:09:283471browse

The difference between member variables and local variables:

1. Different positions in the class

Member variables: in the class, outside the method

Local variables: in In the method definition or method declaration

2. The location in the memory is different:

Member variables: in the heap memory

Local variables: in the stack memory

3. Different life cycles:

Member variables: exist with the creation of the object, and disappear with the disappearance of the object

Local variables: exist with the call of the method , disappears as the method call is completed

4. Different initialization values

Member variables: have default initialization values

Local variables: no default initialization values, must be defined, Assign a value before it can be used.

Note:

The local variable name can be the same as the member variable name. When used in a method, the proximity principle is adopted.

The above is the detailed content of The difference between member variables and local variables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn