< Prev
Next >



Java final keyword





The main feature of the final keyword is, when anything marked with the final keyword is initialized to a value, this initialized value cannot be changed i.e. the value becomes final or a constant value.





Final local variable


Local variable are declared within a method. Once a local variable declared with a final keyword is initialized with a value, you can't change that value. Although, if you still go ahead and assign a new value to the local variable in a method, it causes a compile error. Let's go ahead and see an example :

class A
{
public static void main(String... ar)
{
final int i=10; //final variable i is initialized to 10.

i=20; //assigning a new value(20) to final variable, i
}
}


Post Compilation :


A.java:7: error: cannot assign a value to final variable i
i=20;
^
1 error

In this code, we have initialized a local final variable, i, of int type to the value, 10. In the next statement, we have assigned a new value(20) to final variable, i, which was already initialized to the value, 10, causing a compile error.




Final instance variable


Once a value is given to the final instance variable of a class, you cannot change its value. Although, if you still go ahead and give a new value to the instance variable of a class, it causes a compile error. Let's go ahead and see an example :

class A
{
final int i=10;

public static void main(String... ar)
{
A ob= new A();
ob.i=20;
}

}


Post Compilation :


A.java:8: error: cannot assign a value to final variable i
ob.i=20;
  ^
1 error






Final method


Once a method is declared final in a class, it cannot be redefined or overridden by a subclass. But if we still try to override or give a new definition to a final method of a class, compiler will throw a compile error. Let's see an example -:

class Car
{
public final void speed()
{
}

}
class Ferrari extends Car
{
public void speed()
{
}

}


Post Compilation :


public void speed()
            ^
  overridden method is final
1 error

Here, we have made a class Car with a method speed() declared as final. Another class, Ferrari has extended the class Car and has overridden its final method speed(), causing the compile error.




Use of a final method


Marking a method with the final keyword in class, enforces its subclasses to use the final method in its original form by disabling them from overriding this final method.




Final class


Once a class is declared as final, you cannot extend it, i.e. declaring a class as final, stops it from being inherited by another class. Let's see an example.

final class Car
{
public void speed()
{
}

}
class Ferrari extends Car
{
}


Post Compilation :


Ferrari.java:1: error: cannot inherit from final Car
class Ferrari extends Car
                      ^
1 error

We have declared a class Car as final, which makes Car a final class i.e. it cannot be extended or inherited by another class. But still, we have made another class, Ferrari, which is extending the final class, Car, causing the compile error.

Note : In, Java, String class is declared as a final class, hence it cannot be extended or inherited by another class.




Use of a final class


When the creator of a class feels that his/her version of the class is the best version and it can't and shouldn't be improved or modified by any other class by inheriting it, he/she declares this class with the final keyword. Although, declaring a class final prohibits inheritance, which is one of the main benefits of an object oriented language.



Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< Interface
Static keyword >

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page

Notifications



COMING SOON
-
PYTHON