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.

Answer from AmitG on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › final-keyword-in-java
final Keyword in Java - GeeksforGeeks
The final keyword is a non-access modifier used to restrict modification. It applies to variables (value cannot change) methods (cannot be overridden) and classes (cannot be extended). It helps create constants, control inheritance and enforce fixed behavior.
Published   July 22, 2014
🌐
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 Java, the final keyword is used to denote constants. It can be used with variables, methods, and classes. Once any entity (variable, method or class) is declared final, it can be assigned only once. That is, the final variable cannot be reinitialized with another value ... In Java, we cannot change the value of a final variable. For example,
🌐
DataCamp
datacamp.com › doc › java › final
final Keyword in Java: Usage & Examples
Design: Use final classes to prevent inheritance when designing immutable classes or when you want to ensure that the class's implementation cannot be altered. Static Final Variables: Unlike instance final variables, static final variables cannot be assigned via constructors.
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.

🌐
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.
🌐
Javatpoint
javatpoint.com › final-keyword
Final Keyword In Java
final keyword. The final is a keyword in java. Final can be variable, method, class or parameter. Let's see what are there usage.
🌐
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.
Find elsewhere
🌐
Tutorialspoint
tutorialspoint.com › java › final_keyword_in_java.htm
Java - final Keyword
Tester.java:10: error: cannot inherit from final FinalTester public class Tester extends FinalTester { ^ 1 error · The final keyword can be used with variables, methods, and classes to make them constant so that their value or properties cannot be changed.
🌐
Quora
quora.com › In-Java-what-does-final-do
In Java, what does 'final' do? - Quora
Answer (1 of 18): Final Keyword in Java The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be: 1) final variable 2) final method 3) final class 1) final variable final variables are nothing but constants. We cannot change the ...
🌐
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
🌐
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 ... you might want to make the getFirstPlayer method in this ChessAlgorithm class final: class ChessAlgorithm { enum ChessPlayer { WHITE, BLACK } ... final ChessPlayer getFirstPlayer() { return ChessPlayer.WHITE; } ......
🌐
CodeGym
codegym.cc › java blog › keywords in java › java final keyword
Final Keyword In Java
January 14, 2025 - For a class, the final keyword means that the class cannot have subclasses, i.e. inheritance is forbidden... This is useful when creating immutable (unchangeable) objects. For example, the String class is declared as final.
🌐
Geekster
geekster.in › home › final keyword in java
Final Keyword In Java (with Example)
June 27, 2024 - It can also be used to prevent inheritance or to make a class static. In short, the final keyword is a versatile tool that can be used in a variety of ways. The following are different contexts where the final is used: ... In Java, the final keyword is used to apply restrictions on classes, methods, and variables.
🌐
BeginnersBook -
beginnersbook.com › home › java › final keyword in java – final variable, method and class
Final Keyword In Java – Final variable, Method and Class
November 5, 2022 - Lets have a look at the below code: class Demo{ final int MAX_VALUE=99; void myMethod(){ MAX_VALUE=101; } public static void main(String args[]){ Demo obj=new Demo(); obj.myMethod(); } } ... Exception in thread "main" java.lang.Error: Unresolved compilation problem: The final field Demo.MAX_VALUE ...
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › final-keyword-in-java
Final Keyword in Java: A Comprehensive Guide
The Final Keyword in Java is used to declare constants, prevent method overriding, restrict class inheritance and ensure immutability and security. This blog explains how final variables maintain constant values, how final methods prevent overriding, and how final classes restrict inheritance. Dive in for practical examples ...
🌐
DataCamp
datacamp.com › doc › java › static-and-final-keyword
Static and Final Keyword
In this example, counter is a static variable, and incrementCounter is a static method. Both can be accessed without creating an instance of StaticExample. The final keyword is used to declare constants, prevent method overriding, and restrict inheritance.
🌐
Scaler
scaler.com › topics › java › final-keyword-in-java
final Keyword in Java - Scaler Topics
January 24, 2022 - So, if we try to inherit a class which is declared as a final, the system will throw a compile time error. Classes like Integer, String, and many other wrapper classes are an example of Final classes.
🌐
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.
🌐
Codedamn
codedamn.com › news › java
What is 'final' keyword in Java?
November 19, 2023 - In Java, the keyword ‘final’ serves as a non-access modifier applicable to classes, methods, and variables. A ‘final’ class cannot be subclassed, a ‘final’ method cannot be overridden, and a ‘final’ variable cannot be reassigned once initialized. It...