Sitemap
Javarevisited

A humble place to learn Java and Programming better.

Final Keyword In Java

--

We use Final keyword in three context in Java :-

  1. variable
  2. method
  3. class
  1. Final Variable In Java
  • Once Initialized a final variable can’t be reassigned a different value or object.
  • The value of object referenced by final variable can change but the object being referenced by final variable can’t change.
  • A Blank Final Variable Can be Initialized only in Constructor.
  • A Static Blank Final Variable Can only be initialized in a static block.
  • Method Final Arguments can’t be reassigned a new value .
final List<String> list = new ArrayList<>();

Here the list variable will always hold the same object but the object can be modified.

2) Java final method

If you make any method as final, you cannot override it.

The Subclasses won’t be able to use those method to demonstrate method overriding feature.

You Can Inherit Final Method , but you can’t override it.

3) Java final class

If you make any class as final, you cannot extend it.

No Class Can extend Final Class.
If Any Class tried to do so , it will fail at Compile Time Only.

Follow me On Twitter to be Updated on my new Article EveryDay ,

Javarevisited
Javarevisited

Published in Javarevisited

A humble place to learn Java and Programming better.

Java Spring Decoded
Java Spring Decoded

Written by Java Spring Decoded

All Articles related to java , spring , Backend Development and System Design.

No responses yet