Final Keyword in Java

Spread the love

A final variable in Java can be used in different contexts which are:

  • final variable

When the final keyword is used with a variable then the value of the variable can’t be changed once assigned. In case no value has been assigned to the final variable then a value can be assigned to it only through the class constructor

  • final method

When a method is declared final then it can’t be overridden by the inheriting class. It is done to implement a standard fuctionality which all sub classes should inherit.

  • final class

When a class is declared as final in Java, it can’t be extended by any subclass class but it can extend other class