Road java learning: 27.final variables, final methods, final classes

Here Insert Picture Description

1.final variable

The final keyword can be used to declare variables, once set is changed, then not be able to change the value of the variable. Typically, the final defined variables constant.
Variable final keyword definition must be declared at the time of assignment, final addition to the basic types of array constants can be modified, it can also be modified object reference.

final double PI = 3.14final Test test = new Test();
final int[] a ={1,2,3,4,5,6};

一旦一个对象引用被修饰为final后,它只能恒定指向一个对象,无法将其改变以指向另一个对象。

一个即是static又是final的字段值占据一段不能改变的存储空间。
在Java中定义全局常量,通常使用public static final修饰,这样的常量只能在定义是被赋值。


2.final method

final的方法不能被重写。
一个定义为private的方法隐式被指定为final类型。
父类中被定义为private final的方法不可以被子类同名方法覆盖,而是生成新方法。


3.final class

定义为final的类不能被继承,如果将某个类设置为final形式,则类中的所有方法都被隐式的设置为final形式,但是final类中的成员变量可以被定义为final或者非final形式。


If wrong, please correct me criticism, comments are welcome.
Each text sentence: reality will tell you do not work hard will be trampled to death by life without looking for excuses, nothing is the reason for the fight.

Published 74 original articles · won praise 180 · views 30000 +

Guess you like

Origin blog.csdn.net/Fdog_/article/details/104747500