Advertisement



< Prev
Next >



Java final keyword





The main feature of the final keyword is that 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


A local variable is 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 example, 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 the 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




Advertisement




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, the 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 a class enforces its subclasses to use the final method in its original form, thereby disabling the subclasses from overriding this final method.




Final class


Once a class is declared final, you cannot extend it. In other words, declaring a class final prevents 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 means that 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: Note: In Java, the 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 shouldn't be improved or modified by any other class by inheriting it, he/she declares this class with the final keyword. But, declaring a class final prohibits inheritance, which is one of the main benefits of an object-oriented language.



Please share this article -





< Prev
Next >
< Interface
Static keyword >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement