- final作为类修饰符
——最终类 (不能有子类)
- 用final修饰方法
——不能被子类重新定义
- 用final定义常量 (必须被初始化)
——只能赋值一次
【注意】 如果将引用类型的变量标记为final,那么该变量固定指向一个对象,但可以改变对象内的属性值。 final意味着不能改变,到此为止!
public final class test {public static int totalNumber=5;public final int id; //errorpublic int weight;public test(int weight) {id=totalNumber++;this.weight=weight;}public static void main(String args[ ]) {final test t=new test(5);t.weight=t.weight+2;t=new test(4); 不允许t.id++; 不允许}}
