一 引言
今天来谈谈final关键字的作用, 虽然有很多博文关于final进行了很深的研究,但还是要去记录下谈谈自己的见解加深下印象。下面直接进入主题:
二 final关键字的作用
1、被final修饰的类不能被继承。
这一点应该很多人都知道也遇到过,经典案例就是java.lang.String类
还有一些常见的类也是被final所修饰的,如下:
基本类型对应的包装类型(如java.lang.Integer、java.lang.Long等)、字符相关类(java.lang.StringBuilder、java.lang.StringBuffer)、系统类(java.lang.Class、java.lang.System)等。就列这些其他就靠自己平时去发现。
那么问题来了,a、为什么final修饰的类不能被继承?答:这是Java语法定义的,没法。
b、这样设计的目的是什么?答:因为类不需要被拓展类的、实现细节不允许改变,估计是为了安全考虑吧。
2、被final修饰的方法不能被重写
其实这也是Java语法规定的,没法做解释。但是仔细回忆,这种情况跟static关键字修饰方法中一个特点类似,也是不能重写(覆盖)。
下面我们看案例(代码经过自己敲出来的才最有印象):
classMyClass{final voidtest(){
System.out.println("FinalClass");
}
}class MyClass2 extendsMyClass {//编译报错:Cannot override the final method from MyClass
public void test(){
System.out.println("FinalClass");
}
}
3、被final修饰的变量不能被“改变”
先说下前提1:被final修饰的变量不像static那样。它也可以修饰局部变量。
前提2:被final修饰的变量一定要被初始化,否则编译不通过。
针对前提,我们先通过案例证明:
1 public classFinalTest {2 //编译失败,不满足前提2。The blank final field count may not have been initialized
3 final intcount;4 public static voidmain(String[] args) {5 //编译通过。前提1:被final修饰的变量不像static那样。它也可以修饰局部变量。
6 final int t = 0;7 }8 }
初始化有两种:直接初始化和在构造函数中初始化(每个构造函数都要初始化即每个实例化对象的入口都要进行初始化)。
public classFinalTest {//直接初始化
final int count = 0;final intnum;//构造函数中初始化,如果没有对num进行初始化,就会编译错误。The blank final field num may not have been initialized
publicFinalTest(){
num= 0;//注释这样就可以看到错误提示信息
}public FinalTest(intt){
num= 0;//this();//这两行左右开启一样才不会报错。
}
}
回归重点,被final修饰的变量,它是什么不能改变呢?变量值还是变量的引用还是两者都不能?看似有点玄乎(是不是自己有些没考虑到),其实也很简单(平时多留意就行)。依次举例证明:
案例1(以基本类型为例):
1 public classFinalTest {2 final int count = 0;3
4 public intgetCount () {5 //The final field FinalTest.count cannot be assigned
6 return count ++;7 }8
9 public static voidmain(String[] args) {10 FinalTest t = newFinalTest();11 System.out.println(t.getCount());12 }13 }
上面代码中第六行报错(The final field FinalTest.count cannot be assigned)了,所以可以得知:对于这种基本类型的变量被final所修饰后,它的值是不能被更改的。
案例2(以对象为例):
1 classCount {2 int count = 0;3 public intgetCount () {4 return ++count;5 }6 }7
8 public classFinalTest {9
10 public static voidmain(String[] args) {11 final Count count1 = newCount();12 final Count count2 = newCount();13 System.out.println(count1.getCount());14 System.out.println(count2.getCount());15 //The final local variable count1 cannot be assigned. It must be blank and not using a compound assignment
16 count1 = count2;17 }18 }
第16行同样的报错信息,但是这个就有点不一样:对象里面的成员的值是可以改变的。所以针对这种对象变量而言,被final修饰后不可变的是变量的引用,而不是变量的内容。
总结下这点:被final修饰的基本类型变量,它的值是不可变的。被final修饰的引用类型变量,它的引用地址是不可变的,对象里的内容是可变的。
三 final关键字的拓展
1、在匿名类中使用外部内的变量,则该变量必须是final所修饰的。下面案例中第10就会编译报错,提示必须是final修饰的变量。
1 public classFinalTest {2
3 public static voidmain(String[] args) {4 int count = 0;5
6 Thread thread1 = new Thread(newRunnable() {7 @Override8 public voidrun() {9 //Cannot refer to the non-final local variable count defined in an enclosing scope
10 count ++;11 }12 });13 }14 }
2、其实final还可以修饰形参。这样做的主要目的是防止无意的修改而影响到调用方法外的变量。如果你没了解这句就说明上面第三点作用你还没了解。
1 classCount {2 int count = 0;3 public intgetCount () {4 return ++count;5 }6 }7
8 public classFinalTest {9 int num = 0;10 public static voidmain(String[] args) {11 final Count count = newCount();12 addCount(count);13 System.out.println(count.count);14 }15 public static void addCount(finalCount count){16 count.getCount();17 //count = new Count();//这种就是篡改。
18 }19 }
3、final变量与普通变量有什么区别,什么时候可以相等?看下下面代码,想下代码输出什么。
1 public classFinalTest2 {2
3 public static voidmain(String[] args) {4 final String str1 = "test";5 final String str2 =getContent();6 String str3 = "test";7
8 String str4 = str1 + "";9 String str5 = str2 + "";10
11 System.out.println(str3 ==str4);12 System.out.println(str3 ==str5);13 }14 public staticString getContent(){15 return "test";16 }17 }
输出后的结果为true和false。这是为什么呢?解释下你就清楚这两者的区别了。如果是final修饰直接定义的字符串或者是基本类型,它在编译期间就会确定其值,则编译器会把它当做常量。所以当有使用到它的地方会直接用常量替换。而其他都是运行时才会确定的值所以依然使用变量去计算。在代码中str2变量,虽然用是final修饰但是它的值要在的运行时才能确定,所以它相当于普通变量。而str5这种计算方式并不是我们想象的简单,因为str2在这里成了普通变量,所以会通过stringBulider去计算整个表达式的值,所以返回也是一个新的str,引用地址变了。所以第12行的输出为false;
4、final与finally 和finalize的区别
finally是异常处理语句结构的一部分,表示最终执行。
finalize是Object类的一个方法,在垃圾收集器执行的时候会调用被回收对象的此方法,供垃圾收集时的其他资源回收,例如关闭文件等。