final单词的含义是最终的,不可改变的,在Java语言中,使用final可以用来修饰类、变量、方法。
// 修饰类 public final class Student{ } // 修饰方法 public final void method(){ } // 修饰变量 private final String name = "张三";
final在开发中的应用场景
final int number = 10; // 后续无法再修改number的值 number = 100; // 报错
class Father{ // 不可以改变的方法(不能被子类重写) public final void method(){ } } // 子类 继承父类 class Son extends Father{ // 子类无法对Father类中的method进行重写 }
final class Father{ // 类不能被继承 } // 报错:Father类不能被继承 class Son extends Father{ }
原文:https://www.cnblogs.com/xulinjun/p/14759394.html