DEV Community

Kanishka Shrivastava
Kanishka Shrivastava

Posted on

#java #oop #programming #100daysofcode

Java Concepts I’m Mastering – Part 10: The final Keyword in Java

Continuing my journey of mastering Java fundamentals.

Today’s concept: The final keyword — small word, strong restrictions.

final is used to prevent modification.

But its behavior depends on where it’s applied.

Final Variable

A final variable cannot be reassigned.


final int MAX_AGE = 100;

// MAX_AGE = 120; // Error

Enter fullscreen mode Exit fullscreen mode

Once assigned → value cannot change.

If it's a reference variable:

final List<String> list = new ArrayList<>();
list.add("Java");   // Allowed
// list = new ArrayList<>(); // Not allowed

Enter fullscreen mode Exit fullscreen mode

You cannot change the reference,
but you can modify the object.

Final Method

A final method cannot be overridden.

class Animal {
    final void sound() {
        System.out.println("Animal sound");
    }
}

Enter fullscreen mode Exit fullscreen mode

Child classes cannot modify this behavior.

Final Class

A final class cannot be inherited.


final class Utility {
    void print() {
        System.out.println("Utility class");
    }
}

Enter fullscreen mode Exit fullscreen mode

No class can extend Utility.

Why It’s Important

Improves security

Prevents unwanted modification

Helps in writing immutable classes

Used heavily in system-level and framework code

What I Learned

final protects design decisions

Use it intentionally, not randomly

It strengthens object-oriented design

Small keyword. Big control.

Next in the series: Exception Handling in Java (try-catch-finally)

Top comments (0)