Maybe a class variable or an instance variable. It means that once the variable is initialized, its value cannot be modified, or changed. Meaning that its value is considered as a constant. Therefore, a final variable must be initialized, as it is declared, Otherwise, compiler will pop up an error. It won't be automatically initialized as a null or a zero by Java.
final class variable should be initialized in a static block; final instance variable can be initialized in a constructor.
A 'final' keyword can be applied on a method
Maybe a class method or an instance method. Final methods can be inherited by sub-classes. However, for a class method it means that the method cannot be hidden; for an instance method it means that the method cannot be overridden.
A 'final' keyword applied on a class
It means that the class cannot be extended; there is no kids allowed.
/** * * @author YNZ */ public final class KeyFinal { public static final int A = 10; public static final int B; public final int x; static { B = 30; } public KeyFinal() { this.x = 20; } public final void doSome() { System.out.println("" + this.x); } public static void main(String[] args) { KeyFinal kf = new KeyFinal(); System.out.println("" + A); kf.doSome(); } }
No comments:
Post a Comment