static final常量

package oo.day06;
//static final常量
public class StaticFinalDemo {
public static void main(String[] args) {
//Aoo.NUM = 250; //编译错误,常量不能修改
//System.out.println(Aoo.NUM);

//1.方法区中加载Boo.class
//2.将NUM1存储在方法区中
//3.去方法区中获取NUM1的值并输出
System.out.println(Boo.NUM1);

//编译器在编译时直接被替换为具体的值,效率高
//等价于System.out.println(6);
System.out.println(Boo.NUM2);
}
}
class Boo{
public static int NUM1 = 5; //静态变量
public static final int NUM2 = 6; //常量
}


class Aoo{
public static final int NUM = 5; //常量
//public static final double PI; //编译错误,必须声明同时初始化
}

原文地址:https://www.cnblogs.com/xiaziteng/p/4731403.html