Java Final Keyword

Java final keyword used as a non-access modifier with Classes, attributes and methods.

After making class as final will not allow us to inherit the class.

Method as final will not allow us override in child class.

Making any variable as final will not allow us to override the value.

Final variable:

When the final keyword is used with any variable, its value can never be changed in the same or derived class.

In the below code snippet, on line number 2, the number is initialized with the value 99 and defined as final. Reassigning a value on line number 5 will give an error “cannot assign a value to the final variable”.

OUTPUT:

Final method:

The method cannot be overridden once declared as final.

In the below code snippet, on line number 4, the function declared with the final keyword will not be eligible to override in the child class at line number 11. It will give the below inline error as shown under the output section.

OUTPUT:

Final Class:

Class cannot be extends or inherit as subclass once declared as final.

In the below code snippet, on line number 4, the class declared with the final keyword will not be eligible to get inherit in the child class at line number 5. It will give the below inline error (cannot inherit from final class_name) as shown under the output section.

OUTPUT:

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan