🌐
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. Read more about attributes our Java Class Attributes Tutorial. ... If you want to use W3Schools services as an educational institution, ...
🌐
W3Schools Blog
w3schools.blog › home › final keyword in java
final keyword in java - W3schools
August 27, 2014 - Exception in thread "main" ... this example. Note: Inside Anonymous classes only final variables are accessible. A method declared with final keyword is known as final method....
🌐
W3Schools
w3schools.in › java › super-final-keywords
Java super and final keyword - W3Schools
... class stud { final void show() ... { books B2 = new books(); B2.show(); } } Once a variable is assigned with the keyword final, it always contains the same exact value....
🌐
W3Schools
w3schools.com › JAVA › java_variables_final.asp
Java Constants (final keyword)
Java Examples Java Compiler Java Exercises Java Quiz Java Server Java Syllabus Java Study Plan Java Certificate ... When you do not want a variable's value to change, use the final keyword.
🌐
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).
Published   July 22, 2014
🌐
W3Schools
w3schools.com › java › ref_keyword_finally.asp
Java finally Keyword
The finally keyword is used to execute code (used with exceptions - try..catch statements) no matter if there is an exception or not. Read more about exceptions in our Java Try..Catch Tutorial.
🌐
Tutorialspoint
tutorialspoint.com › java › final_keyword_in_java.htm
Java - final Keyword
The final keyword is used to define a constant and to make attributes, methods, and classes final i.e., non-changeable. Methods and classes which are defined as final cannot be inherited and overridden. The final keyword is a non-access modifier.
🌐
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.
🌐
DataCamp
datacamp.com › doc › java › final
final Keyword in Java: Usage & Examples
This keyword plays a crucial role in ensuring immutability and preventing inheritance or method overriding. A final variable is a constant; once initialized, its value cannot be changed. ... A final variable must be initialized when it is declared or within a constructor if it is an instance ...
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › core java › the “final” keyword in java
The "final" Keyword in Java | Baeldung
June 14, 2025 - In this tutorial, we’ll take a look at what the final keyword means for classes, methods, and variables. Classes marked as final can’t be extended. If we look at the code of Java core libraries, we’ll find many final classes there.
🌐
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.
🌐
W3docs
w3docs.com › quiz › question › AGD2ZN==
What is the use of the 'final' keyword in Java?
Java is a versatile, object-oriented ... properties is the 'final' keyword. The correct use of 'final' in Java is to create a constant variable or method....
🌐
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)
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. You can declare some or all of a class's methods final. You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses.
🌐
CodeGym
codegym.cc › java blog › keywords in java › java final keyword
Final Keyword In Java
January 14, 2025 - What can we say about an array that is declared final? We know that the String class is immutable: the class is declared final. A string value is stored in a char array that is marked with the keyword final.
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.

🌐
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 - The final keyword in Java programming language serves a specific purpose. It restricts the user from changing a variable, method, or class. For example, when a variable is declared as final, its value cannot be altered after initialization.
🌐
W3Schools Blog
w3schools.blog › home › what is the use of final class in java?
What is the use of final class in java? - W3schools.blog
April 22, 2018 - What is the use of final class in java : A final class is simply a class that can't be extended. Main use of a final class is to prohibit inheritance in Java.
🌐
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 ...
🌐
Studytonight
studytonight.com › java-examples › java-final-keyword
Java Final Keyword - Studytonight
We cannot modify or reassign the value of a final variable. A final method cannot be overridden in any of the subclasses. A final class can't have any subclasses. In this tutorial, we learned how to use the final keyword to put certain restrictions on variables, methods, and classes in Java.