Professional Documents
Culture Documents
6 Final, Interface, Abstract
6 Final, Interface, Abstract
•A java variable can be declared using the keyword final. Then the final variable can be
assigned only once.
•A variable that is declared as final and not initialized is called a blank final variable. A
blank final variable forces the constructors to initialise it.
•Java classes declared as final cannot be extended.!
•Methods declared as final cannot be overridden.
•final parameters – values of the parameters cannot be changed after initialization.
•Java local classes can only reference local variables and parameters that are declared
as final.
•A visible advantage of declaring a java variable as static final is, the compiled java
class results in faster performance. This optimization applies only to primitive types
and String constants, not arbitrary reference types.
•You cannot initialize final static variables inside a constructor.
A final static variable must be definitely initialized either
during declaration or
in a static initialize (static block) of the class in which it is declared otherwise, it
results in a compile-time error.
public class Demo {
final int x;//blank final
Demo() {
x = 2;
}
Demo(int i) {
x = i;
}
}