Final Keyword in Java
Final Keyword in Java
The final keyword in java is used to restrict the user. The java final keyword can be
used in many context. Final can be:
If you make any variable as final, you cannot change the value of final variable(It
will be constant).
Ex:-
class Bike{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike obj=new Bike();
obj.run();
}
}//end of class
Output: Compile Time Error.
2) Java final method
Example:
Program 1:
class Parent{
public void property(){
System.out.println("cash+gold+land");
}
public final void marriage(){
System.out.println("subbalakshmi");
}}
Program 2:
class child extends Parent{
public void marriage(){
System.out.println("Pooja");
}}
OUTPUT:
Compile time error.
D:\Java>javac Parent.java
D:\Java>javac child.java
child.java:3: marriage() in child cannot override marriage() in Parent; overridden
method is final public void marriage(){
Ex-2:-
class Bike{
final void run(){
System.out.println("running");
}
}
Example:
Program 1:
Program 2:
OUTPUT:
D:\Java>javac Parent.java
D:\Java>javac child.java
child.java:1: cannot inherit from final Parent class child extends Parent
Ex-2:-
final class Bike{
void run(){
honda.run();
Note: Every method present inside a final class is always final by default whether
we are declaring or not. But every variable present inside a final class need not be
final.
Example:
static
{
x=999;
}}
Whereas the main disadvantage is we are missing the key benefits of oops:
Ans) Yes, final method is inherited but you cannot override it. For Example:
class Bike{
System.out.println("running...");
new Honda2().run();
Output:running...
For the instance variables it is not required to perform initialization explicitly jvm
will always provide default values.
Example:
class Test{
int i;
System.out.println(t.i);
}}
Output:
D:\Java>javac Test.java
D:\Java>java Test
0
If the instance variable declared as the final compulsory we should perform
initialization explicitly and JVM won't provide any default values. Whether we are
using or not otherwise we will get compile time error.
Example:
Program 1:
class Test{
int i;
Output:
D:\Java>javac Test.java
D:\Java>
Program 2:
class Test{
final int i;
Output:
D:\Java>javac Test.java
Rule:
For the final instance variables we should perform initialization before constructor
completion. That is the following are various possible places for this.
Example:
class Test{
Output:
D:\Java>javac Test.java
D:\Java>
Example:
class Test{
final int i;
i=10;
}}
Output:
D:\Java>javac Test.java
D:\Java>
3) Inside constructor:
Example:
class Test{
final int i;
Test(){
i=10;
}}
Output:
D:\Java>javac Test.java
D:\Java>
If we are performing initialization anywhere else we will get compile time error.
Example:
class Test{
final int i;
i=10;
}}
Output:
D:\Java>javac Test.java
Test.java:5: cannot assign a value to final variable i
i=10;
If the value of a variable is not varied from object to object such type of
variables is not recommended to declare as the instance variables. We have
to declare those variables at class level by using static modifier.
In the case of instance variables for every object a separate copy will be
created but in the case of static variables a single copy will be created at
class level and shared by every object of that class.
For the static variables it is not required to perform initialization explicitly
jvm will always provide default values.
Example:
class Test{
static int i;
System.out.println("value of i is :"+i);
}}
Output:
D:\Java>javac Test.java
D:\Java>java Test
Value of i is: 0
If the static variable declare as final then compulsory we should perform
initialization explicitly whether we are using or not otherwise we will get compile
time error.(The JVM won't provide any default values).
Rule:
For the final static variables we should perform initialization before class loading
completion otherwise we will get compile time error. That is the following are
possible places.
Example:
class Test {
Output:
D:\Java>javac Test.java
D:\Java>
2) Inside static block:
Example:
class Test{
static{
i=10;
}}
Output:
Compile successfully.
If we are performing initialization anywhere else we will get compile time error.
Example:
class Test{
i=10;
}}
Output:
D:\Java>javac Test.java
Example:
class Test {
int i;
System.out.println("hello");
}}
Output:
D:\Java>javac Test.java
D:\Java>java Test
Hello
Example:
class Test{
int i;
System.out.println(i);
}}
Output:
D:\Java>javac Test.java
System.out.println(i);
Even though local variable declared as the final before using only we should
perform initialization.
Example:
class Test{
final int i;
System.out.println("hello");
}}
Output:
D:\Java>javac Test.java
D:\Java>java Test
hello
Note: The only applicable modifier for local variables is final if we are using any
other modifier we will get compile time error.