java final关键字
- final的变量的值不能被修改
- final的成员变量
- final的局部变量
public class TestFinal {
public static void main(String[] args) {
final int i = 0;
i = 3; // error
}
}
- final的方法不能被重写
class Father {
final public void eat() {
System.out.println("Father eat.");
}
}
class Son extends Father {
public void eat() { // error
System.out.println("Son eat.");
}
}
- final的类不能被继承
final class A {}
class B extends A {} // error