Home > Java > javaTutorial > What are the differences between different variables in java

What are the differences between different variables in java

PHPz
Release: 2023-04-28 21:46:05
forward
1099 people have browsed it

Difference

1. Location in the class

Member variables: in the class, outside the method

2. Location in the memory

Member variables: Heap

Local variables: Stack

3. Initialized value

Member variables: have default values

Local variables: no default value, only definition and assignment can be used

Life cycle

Member variables: created with the creation of the object, disappear with the disappearance of the object

Local variables: exist with the call of the method and disappear with the end of the method

Instance

The problem of duplicate names of member variables and local variables , proximity principle;

can be distinguished using the this keyword. this.string refers to the member variables in the class, not within the method.

public class Demo{
    String string= "成员变量";
 
    public static void main(String[] args) {
        new Demo().show();
    }
 
    public void show() {
        String string= "局部变量";
        System.out.println(string);
    }
}
Copy after login

The above is the detailed content of What are the differences between different variables in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
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
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template