Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the "final" keyword in Java work? (I can still modify an object.)

Tags:

java

final

In Java we use final keyword with variables to specify its values are not to be changed. But I see that you can change the value in the constructor / methods of the class. Again, if the variable is static then it is a compilation error.

Here is the code:

import java.util.ArrayList; import java.util.List;  class Test {   private final List foo;    public Test()   {       foo = new ArrayList();       foo.add("foo"); // Modification-1   }   public static void main(String[] args)    {       Test t = new Test();       t.foo.add("bar"); // Modification-2       System.out.println("print - " + t.foo);   } } 

Above code works fine and no errors.

Now change the variable as static:

private static final List foo; 

Now it is a compilation error. How does this final really work?

like image 623
G.S Avatar asked Mar 27 '13 09:03

G.S


People also ask

Can you modify a final object Java?

In Java, we know that String objects are immutable means we can't change anything to the existing String objects. final means that you can't change the object's reference to point to another reference or another object, but you can still mutate its state (using setter methods e.g).

Can you modify the final object set?

When a variable declared as final and assigned a value, we can't change the value of the variable. The final method can't be overridden by the child class. A class that is declared final cannot be extended. In case of final object, fields can be changed but the reference can't be changed.

What happens if a method has the final keyword as modifier in Java?

Final Method in Java The Method with Final Keyword cannot be overridden in the subclasses. The purpose of the Final Method is to declare methods of how's definition can not be changed by a child or subclass that extends it. The above example of the Final Method generates a compile-time error.

How does final keyword work in Java?

In Java, the final keyword can be used while declaring an entity. Using the final keyword means that the value can't be modified in the future. This entity can be - but is not limited to - a variable, a class or a method.


2 Answers

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.

import java.util.ArrayList; import java.util.List;  class Test {     private final List foo;      public Test() {         foo = new ArrayList();         foo.add("foo"); // Modification-1     }      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 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 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 which is created by using the new keyword which will ultimately invoke the Test constructor which changes the value at the time of multiple object creation (Remember static foo is not copied in every object, but is shared between multiple objects.)

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)

like image 182
AmitG Avatar answered Oct 09 '22 03:10

AmitG


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.

like image 30
Marko Topolnik Avatar answered Oct 09 '22 03:10

Marko Topolnik