Professional Documents
Culture Documents
20 Salient Features of Final
20 Salient Features of Final
If final memberVariable is not declared at time of declaration we will face compilation error=
“The blank final field x may not have been initialized”’.
If final memberVariable is assigned a new value we will face compilation error=”The final
field FinalTest.x cannot be assigned ”.
If constructor is defined then final memberVariable can be initialized in constructor but once
initialized cannot be assigned a new value.
class FinalTest {
final int x; //final memberVariable/instanceVariable
FinalTest() {
x = 1; //final memberVariable can be initialized in
constructor.
}
}
Final local variable can be left uninitialized at time of declaration and can be initialized later,
but once initialized cannot be assigned a new value in java.
class FinalTest {
void method(){
final int x; //final variable uninitialized at time of
declaration
x=1;
}
}
Final static variable of class must be initialized at time of declaration or can be initialized in
static block, once initialized final static variable cannot be assigned a new value.
If static block is defined then final static variable can be initialized in static block, once
initialized final static variable cannot be assigned a new value in java.
class FinalTest {
final static int x; //final static
variable
static{ //static block
x=1;
}
}
So, in short using final with variable means that it cannot be assigned a new
value in java.
Final method cannot be overridden, any attempt to do so will cause compilation error.
Runtime polymorphism is not applicable on final methods because they cannot be inherited.
So, in short using final with method means that it cannot be inherited in java.
So, in short using final with class means that it cannot be extended in java.
class FinalTest {
final int x; //memberVariable/instanceVariable
FinalTest() {
x = 1; //final memberVariable can be initialized in
constructor.
}
}
7. Final local variable can be left uninitialized at time of declaration and can be
initialized later, but once initialized cannot be assigned a new value.
class FinalTest {
void method(){
final int x; //uninitialized at time of
declaration
x=1;
}
}
variables in interface are public, static and final by default. Interface variables are also
known as constants.
10. If static block is defined then final static variable can be initialized in static block,
once initialized final static variable cannot be assigned a new value in java.
class FinalTest {
final static int x; //static
variable
static{ //static block
x=1;
}
}
11. Final method cannot be overridden, any attempt to do so will cause compilation
error in java.
13. Final class cannot be extended, any attempt to do so will cause compilation error in
java.
14. Constructor of a class can never be final in java - because they are not inherited
neither overridable.
Only public, protected & private are permitted modifiers with constructor.
,any attempt to make constructor final will cause compilation error=”Illegal modifier for the
constructor in type FinalTest; only public, protected & private are permitted” in java.
15. There is nothing like final object in java - if final reference variable refers to object
in java than it does not mean that Object is final, it simply means that reference
variable cannot refer to another object in java.
16. final list does not means that - value cannot be added to it or removed from list.
final list means reference cannot refer to any new ArrayList, any attempt to do so will
cause compilation error in java.
17. If parameter is final its value cannot be changed, any attempt to do so will cause
compilation error in java.
18. Final class can never be abstract, because final class cannot be extended and
abstract class is meant for extending
any attempt to do so will cause compilation error in java.
19. declaring variable, class or method as final improves performance, because JVM
caches them. JVM caches them because -
a. final variable cannot be changed in java,
b. Runtime polymorphism is not applicable on final methods because they
cannot be overridden in java.
c. final classes cannot be inherited in java.
20. As per java naming conventions final variable must be written in UPPERCASE
because they are considered as constants in java.
final int VARIABLE=1;
21. Only final local variable can be used inside an inner class defined in a method.
Though non-final member variable can be used inside an inner class defined in a method.
class MyClass{}
class MainClass {
int memberVar=0; //member variable
void m(){
final int localVar=0; //final local variable
MyClass myClass=new MyClass(){
void m(){
System.out.println(localVar);
System.out.println(memberVar);
}
};
}
}
Using non-final local variable inside an inner class will produce compilation error =”Cannot
refer to a non-final variable localVar inside an inner class defined in a different method” in
java.
Summary >
In this core java tutorial we learned about final Keyword in java
language.
We learned that Final keyword can be applied to variable, method and class in
java, 1) Final variable - once initialized final memberVariable cannot be
assigned a new value in java
Also we used final with
final memberVariable/instanceVariable
final local variable
final static variable
2) Final method - final method cannot be inherited in java.
3) Final class - final class cannot be extended in java.