keyword from the Java programming language

In the Java programming language, the final keyword is used in several contexts to define an entity that can only be assigned once. Once a final variable has been assigned, it always … Wikipedia
🌐
Wikipedia
en.wikipedia.org › wiki › Final_(Java)
final (Java) - Wikipedia
October 29, 2025 - Java's primitive types are immutable, as are strings and several other classes. If the above construction is violated by having an object in the tree that is not immutable, the expectation does not hold that anything reachable via the final variable is constant. For example, the following code defines a coordinate system whose origin should always be at (0, 0).
🌐
Oracle
docs.oracle.com › javase › tutorial › java › IandI › final.html
Writing Final Classes and Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
You might wish to make a method final if it has an implementation that should not be changed and it is critical to the consistent state of the object. For example, you might want to make the getFirstPlayer method in this ChessAlgorithm class final:
🌐
W3Schools
w3schools.com › java › ref_keyword_final.asp
Java final Keyword
The final keyword is called a "modifier". You will learn more about these in the Java Modifiers Chapter.
🌐
Programiz
programiz.com › java-programming › final-keyword
Java final keyword (With examples)
In this tutorial, we will learn about final variables, methods, and classes with examples. In Java, the final keyword is used to denote constants.
🌐
Baeldung
baeldung.com › home › java › core java › the “final” keyword in java
The "final" Keyword in Java | Baeldung
June 14, 2025 - We can also find many final methods in Java core libraries. Sometimes we don’t need to prohibit a class extension entirely, but only prevent overriding of some methods. A good example of this is the Thread class.
Top answer
1 of 16
667

This is a favorite interview question. With this questions, the interviewer tries to find out how well you understand the behavior of objects with respect to constructors, methods, class variables (static variables) and instance variables.
Now a days interviewers are asking another favorite question what is effectively final from java 1.8.
I will explain in the end about this effectively final in java 1.8.

import java.util.ArrayList;
import java.util.List;

class Test {
    private final List foo; // comment-1    
    public Test() {
        foo = new ArrayList(); // comment-2
        foo.add("foo"); // Modification-1   comment-3
    }

    public void setFoo(List foo) {
       //this.foo = foo; Results in compile time error.
    }
}

In the above case, we have defined a constructor for 'Test' and gave it a 'setFoo' method.

About constructor: Constructor can be invoked only one time per object creation by using the new keyword. You cannot invoke constructor multiple times, because constructor are not designed to do so.

About method: A method can be invoked as many times as you want (Even never) and the compiler knows it.

Scenario 1

private final List foo;  // 1

foo is an instance variable. When we create Test class object then the instance variable foo, will be copied inside the object of Test class. If we assign final foo inside the constructor, then the compiler knows that the constructor will be invoked only once, so there is no problem assigning it inside the constructor.

If we assign final foo inside a method, the compiler knows that a method can be called multiple times, which means the value will have to be changed multiple times, which is not allowed for a final variable. So the compiler decides constructor is good choice! You can assign a value to a final variable only one time.

Scenario 2

private static final List foo = new ArrayList();

foo is now a static variable. When we create an instance of Test class, foo will not be copied to the object because foo is static. Now foo is not an independent property of each object. This is a property of Test class. But foo can be seen by multiple objects and if every object of Test which is created by using the new keyword which will ultimately invoke the Test constructor which changes the value of final static variable at the time of multiple object creation (Remember static foo is not copied in every object, but is shared between multiple objects.). To stop this, compiler knows final static cannot be initialized inside constructor and also cannot provide method to assign object to it. So we have to declare and define final List object at the same place at comment-1 in above program.

Scenario 3

t.foo.add("bar"); // Modification-2

Above Modification-2 is from your question. In the above case, you are not changing the first referenced object, but you are adding content inside foo which is allowed. Compiler complains if you try to assign a new ArrayList() to the foo reference variable.
Rule If you have initialized a final variable, then you cannot change it to refer to a different object. (In this case ArrayList)

final classes cannot be subclassed
final methods cannot be overridden. (This method is in superclass)
final methods can override. (Read this in grammatical way. This method is in a subclass)

Now let's see what is effectively final in java 1.8?

public class EffectivelyFinalDemo { //compile code with java 1.8
    public void process() {
        int thisValueIsFinalWithoutFinalKeyword = 10; //variable is effectively final
        
        //to work without final keyword you should not reassign value to above variable like given below 
        thisValueIsFinalWithoutFinalKeyword = getNewValue(); // delete this line when I tell you.
        
        class MethodLocalClass {
            public void innerMethod() {
                //below line is now showing compiler error like give below
                //Local variable thisValueIsFinalWithoutFinalKeyword defined in an enclosing scope must be final or effectively final
                System.out.println(thisValueIsFinalWithoutFinalKeyword); //on this line only final variables are allowed because this is method local class
                // if you want to test effectively final is working without final keyword then delete line which I told you to delete in above program.  
            }
        }
    }

    private int getNewValue() {
        return 0;
    }
}

Above program will throw error in java 1.7 or <1.8 if you do not use final keyword. Effectively final is a part of Method Local Inner classes. I know you would rarely use such effectively final in method local classes, but for interview we have to be prepared.

2 of 16
615

You are always allowed to initialize a final variable. The compiler makes sure that you can do it only once.

Note that calling methods on an object stored in a final variable has nothing to do with the semantics of final. In other words: final is only about the reference itself, and not about the contents of the referenced object.

Java has no concept of object immutability; this is achieved by carefully designing the object, and is a far-from-trivial endeavor.

🌐
DataCamp
datacamp.com › doc › java › final
final Keyword in Java: Usage & Examples
A final class cannot be subclassed, preventing inheritance.
🌐
GeeksforGeeks
geeksforgeeks.org › java › final-keyword-in-java
final Keyword in Java - GeeksforGeeks
Final variables hold a value that cannot be reassigned after initialization.
Published   July 22, 2014
Find elsewhere
🌐
Javatpoint
javatpoint.com › final-keyword
Final Keyword In Java
Java Program to divide a string in 'N' equal parts.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-final-finally-and-finalize
Java final, finally and finalize - GeeksforGeeks
January 6, 2016 - The final keyword in Java is used with variables, methods and also with classes to restrict modification. ... Example: The below Java program demonstrates the value of the variable cannot be changed once initialized.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › final class in java
Final Class in Java | Meaning, Method & Example Explained
June 24, 2025 - The final method in Java is a method that cannot be overridden by any subclass. Once a method is declared as final in a superclass, it is considered a complete and unchangeable implementation of that method. Final methods are often used when you want to prevent subclasses from modifying or altering the behavior of a particular method defined in a superclass. It provides a way to enforce consistency and prevent method overriding. Here is an example of using the final keyword with a method in Java:
🌐
Whitman College
whitman.edu › mathematics › java_tutorial › java › javaOO › final.html
Writing Final Classes and Methods
For example, if you wanted to declare your (perfect) ChessAlgorithm class as final, its declaration would look like this: final class ChessAlgorithm { . . . } Any subsequent attempts to subclass ChessAlgorithm will result in a compiler error such as the following: Chess.java:6: Can't subclass ...
🌐
Scientech Easy
scientecheasy.com › home › blog › final keyword in java: use, example
Final Keyword in Java: Use, Example - Scientech Easy
July 30, 2025 - The syntax to declare a final variable in Java is as follows: final data_type variable_name = value; // Just add a final keyword in front of variable definition. For example: final float pi = 3.14f; // Declaration of variable with final keyword.
🌐
Tutorialspoint
tutorialspoint.com › java › final_keyword_in_java.htm
Java - final Keyword
You declare methods using the final modifier in the class declaration, as in the following example − · class FinalTester { int value = 10; public final void changeValue() { value = 12; } } public class Tester extends FinalTester { public void changeValue() { value = 14; } public static void ...
🌐
Abhi Android
abhiandroid.com › java › final-keyword-examples.html
Final Keyword In Java With Examples | Abhi Android
Several classes in Java are final e.g. String, Integer and other wrapper classes. The main purpose or reason of using a final class is to prevent the class from being subclassed. If a class is marked as final then no class can inherit any feature from the final class. ... Let us take a program example to show the use of final class.
🌐
CodeGym
codegym.cc › java blog › keywords in java › java final keyword
Final Keyword In Java
January 14, 2025 - import java.lang.reflect.Field; class B { public static void main(String[] args) throws Exception { String value = "Old value"; System.out.println(value); // Get the String class's value field Field field = value.getClass().getDeclaredField("value"); // Make it mutable field.setAccessible(true); // Set a new value field.set(value, "CodeGym".toCharArray()); System.out.println(value); /* Output: * Old value * CodeGym */ } } Please note that if we had tried to change the final variable of a primitive type in this way, then nothing would have happened. I suggest that you convince yourself: create a Java class, for example, with a final int field and try to change its value using the Reflection API.
🌐
Simplilearn
simplilearn.com › home › resources › software development › java tutorial for beginners › final keyword in java: all you need to know
Final Keyword in Java: All You Need to Know
March 16, 2025 - Dive into Java's 'final' keyword! Understand its nuances, best practices, and vital role in ensuring code integrity. Elevate your Java skills. Click to learn
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Unstop
unstop.com › home › blog › final keyword in java | syntax, uses & more (+code examples)
Final Keyword In Java | Syntax, Uses & More (+Code Examples) // Unstop
October 29, 2024 - Yes, you can declare an array as final in Java. However, there’s an important distinction to note: Reference Immutability: Declaring an array as final prevents you from reassigning the array reference to a different array. This means that once you’ve assigned an array to a final variable, you cannot point that variable to a new array. Element Mutability: While you cannot change the array reference, you can still modify the individual elements within the array. For Example-
🌐
IONOS
ionos.com › digital guide › websites › web development › java final
How to use Java final for classes, methods and variables
January 3, 2025 - This is particularly important if certain variables should remain constant within the code. In the following example, we’re going to create the variable x, which has the value 5.
🌐
GeeksforGeeks
geeksforgeeks.org › java › final-variables-in-java
final variables in Java - GeeksforGeeks
July 23, 2025 - ... // Java Program to illustrate ... 2 // main class class GFG { // Main driver method public static void main(String args[]) { final Helper t1 = new Helper(); t1.i = 30; // Works // Print statement for successful execution ...