7.变量

变量

public class Demo08 {
    //类变量 static
    static double salary=2500;
    //属性:变量
    //实例变量:从属于对象;如果不自行初始化,这个类型的默认值
    // 所有的数值类型:0 0.0
    //布尔值:默认是false
    //除了基本类型,其余的默认值都是null
    String name;
    int age;
    //main方法
    public static void main(String[] args) {
        //局部变量;必须声明和初始化值
        int i =10;
        System.out.println(i);
        //变量类型 变量名字 =new Demo08()
        Demo08 demo08=new Demo08();
        System.out.println(demo08.age);
        System.out.println(demo08.name);
        //类变量 static
        System.out.println(salary);

    }
    //其他方法
    public void  add(){

    }
}

常量

用final修饰

public class Demo09 {
    //修饰符 不存在先后顺序
    static final double PI=3.14;
    public static void main(String[] args) {
        System.out.println(PI);
    }
}
posted @ 2022-03-24 23:02  未末丶  阅读(49)  评论(0)    收藏  举报