Fundamentals 3 min read

Understanding the final Keyword in Java

The article explains Java's final keyword, detailing its three distinct uses—modifying variables, methods, and classes—while describing how final variables become immutable constants, final methods cannot be overridden, and final classes cannot be subclassed, illustrating each case with examples.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Understanding the final Keyword in Java

final is a keyword in Java that indicates something cannot be changed. It has three usages: it can modify variables, methods, or classes.

final variables

When a variable is declared as final, its value cannot be changed after assignment. It is typically used for constants or variables that should remain unchanged.

final can modify member variables, static variables, local variables, and method parameters.

final methods

A method declared as final cannot be overridden by subclasses, ensuring the implementation remains unchanged. Template design patterns often use final methods to prevent core algorithmic logic from being overridden.

final classes

A class declared as final cannot be subclassed, guaranteeing that its implementation cannot be extended or altered. Java's immutable String class is an example of a final class.

Summary

When final is applied to a variable, method, or class, its meaning differs: for variables it makes the value immutable after assignment; for methods it prevents overriding; for classes it prevents inheritance.

JavaClassesMethodsfinal keyword
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.