Java – Final Keyword

PREV     NEXT

The keyword ‘final’ in java is used to restrict access of an entity.  The keyword ‘final’ can be used with,

  • Variable
  • Method
  • Class

Final Variable:

If a variable is declared as final, its value cannot be changed. Final variable can be initialized only once – either at the declaration time or inside a constructor. Final variables are similar to constants.

Example:


COMPILE & RUN

Note:

  • A blank final variable is a final variable, which is not initialized while declaration. It can be assigned only once.
  • Both Parameters and local variables can also be declared as final. Value of such final members cannot be changed within the method.

Final Method:

When a method is declared as final, it cannot be overridden in any of its subclasses. But, overloading is possible with final method.


Example:

COMPILE & RUN

Final Class:

When a class is declared as final, it cannot be inherited further.

Example:

COMPILE & RUN

PREV     NEXT

Share this website !!!
Facebook Twitter Linkedin