Final Keyword in Java

Final Keyword in Java

May 11th, 2026
2850
10:00 Minutes

Final Keyword in Java is a non-access modifier used to restrict the user from changing variables, methods and classes. It enforces immutability and prevents inheritance, which makes it one of the most important concepts a Java developer should know about. Are you aware of it? No problem! This guide will explain everything a beginner needs to know about the final Keyword in Java.

What is the final keyword in Java?

The final keyword is a special type of keyword in Java that can prevent something from being changed or modified. Its non-access modifier nature makes classes, methods and attributes non-changeable. This means it becomes impossible to inherit or override them. In simple words, final helps secure your application by ensuring that certain data and behaviors remain constant. This makes your program more stable, predictable and reliable.

Master Java Programming with Java Training

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

Different Ways to Use the final keyword in Java

The final keyword is used in exactly 3 main contexts, which are explained below:

1. final Variables

This value cannot be changed, so when a variable is declared final, its value becomes constant. For example:

class FinalVariableExample {
    public static void main(String[] args) {
        final int MAX_SPEED = 120;
        System.out.println(MAX_SPEED);

        // MAX_SPEED = 150;   ERROR: cannot assign a value to final variable
    }
}

In this code,

  • MAX_SPEED is fixed.
  • If you try to change it, then it will show a compile-time error.

final variables in java

Types of final Variable

1. final Variable

A final variable is a variable whose value is fixed.

final int THRESHOLD = 5;

THRESHOLD will always stay 5

2. Blank final Variable

A blank final variable is declared without a value at first.

final int THRESHOLD;

After it gets a value, it becomes constant and this cannot be modified.

3. Static final Variable

A static final variable belongs to the class, not to any object.

static final double PI = 3.141592653589793;

4. Static Blank final Variable

A static blank final variable is a class-level constant that is not initialized immediately.

static final double PI;

Also Explore: Multithreading in Java

2. Final Methods

A final method cannot be changed or overridden by subclasses.

class Parent {
    final void show() {
        System.out.println("This is a final method");
    }
}

class Child extends Parent {
    // Cannot override final method
}

public class Main {
    public static void main(String[] args) {
        Child obj = new Child();
        obj.show(); // calls Parent's final method
    }
}


}

final methods in java

Related Article: Volatile Keyword in Java

3. Final Classes

When a class is final, no subclass can extend it.

final class Animal {
    void eat() {
        System.out.println("Animal is eating");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Animal();
        a.eat();
    }
}

final classes in java

Read Also: What are Classes and Objects in Java

4. Effectively Final Variables (Java 8+)

From Java 8 onwards, Java introduced the concept of effectively final variables. A variable is considered effectively final when its value is assigned only once and never changed afterwards, even if it is not explicitly declared using the final keyword. This concept is mainly used in lambda expressions and anonymous inner classes. If you try to modify such a variable after initialization, the compiler will throw an error. In simple terms, Java treats the variable as final even though you did not write the final keyword.

Let’s understand this with an example:

public class EffectivelyFinalExample {
    public static void main(String[] args) {

        int number = 10;  // Effectively final

        Runnable r = () -> {
            System.out.println(number);
        };

        r.run();

        // number = 20;  // ERROR: Variable used in lambda should be final or effectively final
    }
}


In this example:

  • number is not declared as final.
  • However, its value is never changed after initialization.
  • Therefore, Java treats it as effectively final.
  • If you try to modify it later, the code will not compile.

This feature improves code readability because you do not always need to write the final keyword explicitly. However, the rule still applies — once used inside a lambda expression, the variable must not be modified.

Key Features of the final keyword in Java

Let's explore some of the common features of the final keyword in the Java programming language:

  • Immutability: When you use final with a variable, it means the value is fixed. Once you set it, you cannot change it later. It stays the same throughout the program.
  • Security: When a method is marked as final, no other class can change how that method works. This helps protect important logic and keeps things reliable.
  • Design: When a class is marked as final, no one can inherit from it. This is useful when you want the class to work in only one way and not be modified.
  • Static final Variables: These are constants that belong to the class, not to any object. Their value is set only once (usually when declared) and cannot be changed later, even inside a constructor.

public class AppConfig {
    public static final int MAX_USERS = 100;
}
  • Performance: The use of final can help the Java compiler and JIT compiler to optimize the code, as it provides more information about the invariability of the value.

Read Also: What are Constructors in Java

Want to Learn Everything About Java Programming?

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

Wrapping Up

In this guide, I have explained the final keyword in Java, including final Variables, methods, and classes. Now you know how this keyword improves security, design, immutability and performance, which helps beginners write cleaner, safer and more reliable Java applications with better coding practices. Further, you can also explore our Java tutorial for better understanding of other concepts.

Explore Our Trending Articles-

FAQs on the final keyword in Java

1. What is the use of the final keyword in Java?

It is used to make sure something (a variable, method or class) cannot be changed.

2. What are final, finally, and finalize() in Java?

final prevents changes, finally always executes after try-catch, and finalize() was used for cleanup before garbage collection but has been deprecated since Java 9 and is no longer recommended.

3. What does final do for a method?

It stops other classes from overriding that method.

4. Is final the same as const?

Java does not use const, so in that case it uses final to create values that should never change.

5. How does final relate to multithreading?

It helps keep data safe by ensuring values don’t change when multiple threads are running.

About the Author
Author Nehal Sharma
About the Author

Nehal Sharma is a skilled Data Analyst with expertise in Java, mobile development, and data analytics. She transforms complex data into actionable insights and has experience in business intelligence, data science, and Salesforce. She also simplifies technical concepts into clear, engaging content for learners and professionals.

Drop Us a Query
Fields marked * are mandatory
×

Your Shopping Cart


Your shopping cart is empty.