Final Keyword in Java

  • Final keyword can be applied to variable, method and class. final member cannot be changed.
  • A variable that is declared as final and not initialized is called a blank final variable. A blank final variable forces the constructors to initialize it.
  • Java classes declared as final cannot be extended. Restricting inheritance.
  • Final parameters - values of the parameters cannot be changed after initialization.
  • Final method – can not be overridden.

A visible advantage of declaring a java variable as static final is that the compiled java class results in faster performance. Final is a ‘non-access modifier’ which can only be applied to a variable, method or class. It is basically unchangeable, as in , its value once assigned cannot be changed. The final keyword may be used in various different scenarios.


Example 1: Final variable can not be changed

Example 2: Final method cannot be overridden

Example 3: The final class cannot be extended

Note:

  • Constructor cannot be final
  • Block cannot be final

Some more Examples