Skip to main content

What is final keyword in Java.

Where can Finally keyword be used ??
Finally keyword can be  used with the following
  • final variable, a variable marked as final acts like a constant variable whose value can not be changed once assigned throughout the life of the variable.
  • final method, a method marked as final can never be overridden but can be overloaded. 
  • final class, a class marked as final can never be extended.
  • final object, final object is an object whose reference can not be changed once assigned.
Final variable
final variable when used with any variable, then this variable starts acting like a constant. Value of this variable can not be changed throughout the life of this variable.Suppose we have a final variable in a class and we try to change the value of this variable.
public class TestClass{

final int test = 1;

public static void main(String args[]){

TestClass obj = new TestClass();
obj.test = 2;
System.out.println(obj.test);
}

}
The output of the above program will be
TestClass.java:9: error: cannot assign a value to final variable test
Final method
When a method is marked as final then this method can not be overridden.
class FinalClass
{
public final void show(){

System.out.println("Original method");
}
}

public class HelloWorld extends FinalClass{

public static void main(String args[]){

FinalClass obj = new FinalClass();
obj.show();
}

public final void show(){

System.out.println("Overridden method");
}

}
The Output of the  above program is
HelloWorld.java:17: error: show() in HelloWorld cannot override show() in FinalClass
Thus this is clear that a final method cannot be overridden. However a final method can be overloaded.

public class HelloWorld{

public static void main(String args[]){

HelloWorld obj = new HelloWorld();
obj.show();
obj.show("test");
}

public final void show(){

System.out.println("show");
}

public final void show(String str){

System.out.println("show(String str)");
}

}
The Output of the  above program is
show
show(String str)
Thus this shows that a final method can be overloaded.
Final class
final keyword when used with the class, then this class can never be inherited by any class. Suppose we have a final class FinalClass and we try to extend it.
final class FinalClass
{}

public class HelloWorld extends FinalClass{

int instanceVarible = 1;


public static void main(String args[]){

System.out.println("HelloWorld");
}
}
The output of the above program is
HelloWorld.java:4: error: cannot inherit from final FinalClass
Thus it is clear that a final class cannot be extended i.e. a final class cannot be inherited.
Final object
when an object is marked as final, the the reference of this object can not be changed. Remember that the reference of this object cannot be changed but the not final member of this object can still be changed.
Example - 1 Shows that a final object's reference can not be changed.


public class HelloWorld{

int test = 100;

public static void main(String args[]){

final HelloWorld obj = new HelloWorld();
obj.test = 50;
System.out.println(obj.test);
}

}
The output of the above program is : 50

Example - 2 Shows that the value of member variables of a final object can be changed.
public class HelloWorld{

int test = 100;

public static void main(String args[]){

final HelloWorld obj1 = new HelloWorld();
HelloWorld obj2 = new HelloWorld();
obj1 = obj2;
}

}
The output of the above program is
HelloWorld.java:9: error: cannot assign a value to final variable obj1
Thus from the above examples it is clear that the reference to the object is constant as shown in example-2, we were not able to reference obj1 to any other object. However in example-1, the object obj was marked as final but still its member variable was changed, this is because the member variables are still a reference to the objects say String or Integer for example. Thus the value of these objects can be changed.

Comments