Java变量作用域(Java variable scope)

类变量

static 变量类型 变量名 = 初始值;

    static double salary = 2500;
    System.out.println(salary);

实例变量

从属于对象:如果不自行初始化,基本类型的默认值为 0 0.0
boolean值得默认值是false
除了基本类型,其余的默认值都是null

  public class Demo07 {
    String name; 
    int age; 
    public static void main(String[] args) {
      // 变量类型(可自定义)  变量名字 = new Demo07
      Demo07 demo07 = new Demo07();
      System.out.println(demo07.age); // 0
      System.out.println(demo07.name); // null
    }
  }

局部变量

必须声明和初始化值

public static void main(String[] args) {
        int i = 10;
        System.out.println(i);
} // 只能在这个大括号范围内使用该变量
————————

Class variable

Static variable type variable name = initial value;

    static double salary = 2500;
    System.out.println(salary);

Instance variable

Subordinate object: if it is not initialized by itself, the default value of the basic type is 0.0
The default value of Boolean is false
The default values are null except for the basic type

  public class Demo07 {
    String name; 
    int age; 
    public static void main(String[] args) {
      // 变量类型(可自定义)  变量名字 = new Demo07
      Demo07 demo07 = new Demo07();
      System.out.println(demo07.age); // 0
      System.out.println(demo07.name); // null
    }
  }

local variable

Values must be declared and initialized

public static void main(String[] args) {
        int i = 10;
        System.out.println(i);
} // 只能在这个大括号范围内使用该变量