Java的final关键字

final 关键字的一般用法:

  • 修饰类,表示不可被继承
  • 修饰方法,表示不可被重写
  • 修饰变量,表示赋值后不可更改
    final修饰变量时,不可更改指的是对象的位置,如果是一个数组,数组内的内容是可以修改的.
 public static void main(String[] args) {
        final int a=1;
       // a=2; 不可更改
        final String b="b";
        //b = "c";  不可更改
        final String c = new String("c");
        //c = "a";
//        c = new String("a");  不可更改
        SIngleTonA single = new SIngleTonA();
//        single.e=2;  不可更改
        final int [] f={1,2,3};
        System.out.println(f[0]);
        f[0]=2;
        System.out.println(f[0]);

    }

posted @ 2020-03-08 19:35  少年留不住  阅读(120)  评论(0编辑  收藏  举报