final定义的变量是只读的,一旦定义将不能被改变,定义类,他不能被继承,用于方法中,不能被重写,定义变量,变量不可变- public static final String LOAN = "loan" ;
- LOAN = new String( "loan" ) //invalid compilation error
- //上面是定义变量的
- class PersonalLoan{
- public final String getName(){
- return "personal loan" ;
- }
- }
- class CheapPersonalLoan extends PersonalLoan{
- @Override
- public final String getName(){
- return "cheap personal loan" ; //compilation error: overridden method is final
- }
- }
- //上方是定义方法
- final class PersonalLoan{
- }
- class CheapPersonalLoan extends PersonalLoan{ //compilation error: cannot inherit from final class
- }
- //上方是定义类的
复制代码 |