Java final 关键字

Final 关键字

final 关键字在 Java 中被称为完结器,表示最终的意思

并且,final 能声明类、方法和属性,但是:

1. 使用 final 声明的类不能被继承;

2. 使用 final 声明的方法不能被重写;

3. 使用 final 声明的变量变成敞亮,常亮是不可以被修改的。

 

代码如下:

package hello;

final class People2{
	final public void tell(){
		
	}
}

class student2 extends People2{
	public void tell(){
		
	}
}

public class FinalDemo {

	public static void main(String[] args) {
		final String NAME = "zhangsan";
		name = "lisi";
	}

}  

上述代码会报三个错误:

The type student2 cannot subclass the final class
People2

Cannot override the final method from
People2

name cannot be resolved to a
variable

 

一般的,用 final 修饰的变量均要大写(NAME)

 

posted @ 2017-04-22 12:57  苌来看看  阅读(170)  评论(0编辑  收藏  举报