java final 关键字使用

来源:互联网 发布:凌波城静脉不动的算法 编辑:程序博客网 时间:2024/05/22 11:58
package finalTest;public class Test {    public static final int num1 = 1;//常用方式1    public final int num2 = 2;//常用方式2//  public final Integer num4;//final修饰的变量不初始化直接报错    public final Integer num5;    public int num3 = 3;    public static  final int num6;    static {        num6=6;//必须要有初始化才不会报错    }    public Test(){        num5=5;//必须要有初始化才不会报错    }    public static void main(String[] args) {        Test t = new Test();        // t.num1 = 6; final修饰的变量修改直接编辑器报错        // t.num2 = 6; final修饰的变量修改直接编辑器报错        t.num3=6;//没有final修饰的变量编辑器才不会报错    }}
原创粉丝点击