Fundamentals 7 min read

Understanding the final Keyword in Java: Variables, Methods, and Classes

This article explains Java's final keyword, covering its use with classes, methods, and variables, demonstrates how to declare final members, shows code examples of final variables, methods, and classes, and discusses the performance, thread‑safety, and immutability benefits of using final in Java applications.

Java Captain
Java Captain
Java Captain
Understanding the final Keyword in Java: Variables, Methods, and Classes

Java's final keyword is important and can be applied to classes, methods, and variables. This article explores what final means, what declaring variables, methods, and classes as final represents, the benefits of using final, and provides examples.

Meaning of the final keyword

final is a reserved keyword in Java that can declare member variables, methods, classes, and local variables. Once a reference is declared final, it cannot be changed; the compiler will report an error if you try to reinitialize it.

What is a final variable?

Any member or local variable declared as final is called a final variable. Final variables are often used with static to define constants. Example:

public static final String LOAN = "loan";
// LOAN = new String("loan"); // invalid compilation error

Final variables are read‑only.

What is a final method?

A method declared final cannot be overridden by subclasses. If a method's functionality is complete and does not need alteration, declare it final. Final methods are faster because they are statically bound at compile time.

class PersonalLoan{
    public final String getName(){
        return "personal loan";
    }
}
class CheapPersonalLoan extends PersonalLoan{
    @Override
    public final String getName(){ // compilation error: overridden method is final
        return "cheap personal loan";
    }
}

What is a final class?

A class declared final cannot be subclassed. Many Java classes such as String and wrapper classes are final.

final class PersonalLoan{
}
class CheapPersonalLoan extends PersonalLoan{ // compilation error: cannot inherit from final class
}

Benefits of the final keyword

Using final provides several advantages:

Improves performance because the JVM can cache final variables.

Allows safe sharing of final variables across multiple threads without additional synchronization.

Enables JVM optimizations for methods, variables, and classes.

Immutable classes

Creating immutable classes typically involves using final. Immutable objects, such as String, cannot be changed after creation, offering thread‑safety and eliminating synchronization overhead.

Important points about final

1. final can be used for member variables, local variables, methods, and classes.

2. Final member variables must be initialized at declaration or in a constructor.

3. Final variables cannot be reassigned.

4. Local final variables must be assigned when declared.

5. All variables used in an anonymous class must be final.

6. Final methods cannot be overridden.

7. Final classes cannot be subclassed.

8. final is different from finally, which is used for exception handling.

9. final should not be confused with finalize(), a method called before garbage collection.

10. All variables declared in an interface are implicitly final.

11. final and abstract are mutually exclusive; a class cannot be both.

12. Final methods are bound at compile time (static binding).

13. A blank final variable (not initialized at declaration) must be initialized in a constructor; otherwise, compilation fails.

14. Declaring classes, methods, or variables as final can improve performance by allowing JVM optimizations.

15. By convention, final variables that are constants are written in uppercase.

private final int COUNT = 10;

16. For collection objects, final means the reference cannot change, but the collection's contents can be modified.

private final List loans = new ArrayList();
loans.add("home loan"); // valid
loans.add("personal loan"); // valid
loans = new Vector(); // not valid

Using final when appropriate can lead to faster, more reliable Java code.

JavaperformanceThread Safetyimmutabilityfinalkeyword
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

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.