07_变量和常量

  1. 类变量,实例变量,局部变量
public class Hello {
    // 类变量 static
    static double salary = 3000;

    // 实例变量,从属于对象
    // 布尔值默认为false
    String name; // 默认值为null
    int age; // 默认值为0

    public static void main(String[] args) {
        // 局部变量,必须声明和初始化值
        int i = 1;
        Hello hello = new Hello();
        hello.age = i;
        System.out.println(salary);
    }
}
  1. 常量 final
public class Hello {
    
    // 修饰符,不存在先后顺序
    static final double PI = 3.14159265358979323846;

    public static void main(String[] args) {
        System.out.println(PI);
    }
}
posted @ 2024-06-29 20:46  神莹  阅读(4)  评论(0)    收藏  举报