final 表示最终,最后的意思,
它可以用来修饰一个变量,方法,和类,表示所修饰的对象不可以改变。
1. final 修饰变量:表示这个两个只能被赋值一次。
注意如下案例是可以的:
package com.example.test; public class myClass { public static void main(String[] args){ final String str; str = "one"; System.out.println("str = "+str); } }
输出:
str = one Process finished with exit code 0
3. final 修饰方法: 表示这个方法不能被重写。
所以final 不能被用来修饰抽象类,因为抽象类不能被实例化,需要被继承。
3. final 修饰类:表示该类不能被继承,没有子类。
这是头脑风暴随机想出来的,后面会不停补充