Java Final Keyword

It is a java 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, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.

0