Can we change the value of a final variable of a mutable class?

48,375

Solution 1

final does not enforce any degree of constancy of the object to which a reference is referring (it is not the same as const in C++).

When you mark a variable as final, it means that you cannot assign a different object reference to that variable.

For example, this code will not compile:

final StringBuffer s=new StringBuffer("a");
s = new StringBuffer("another a"); /*not possible to reassign to s*/

It can be useful when using Runnables, Callables and locking idioms.

Solution 2

StringBuffer is mutable according to it's design, and method append is just a method to change it's content.

What final variable means in Java is:

  • For object, it means you can not assign another object to this reference, but it does not stops you from invoking methods of this object.

  • For basic type variables(int, float, boolean etc), since there's no reference and pointer, and object method here, so it simply means you can not change value of the variable.

Take the follow codes

final ObjectA objA = new ObjectA();

You can of course invoke methods of this object like below

objA.setXXX() //legal for a final variable objA

The side effect of setXXX() method is not controlled by final keyword here.

But for the follow code

final int a = 123;

You will not be able to assign any new value to this variable as below

a = 234; //Not legal if a has been defined as a final int variable.

But if you are defining it using Integer as below

final Integer a = new Integer(123);

You will be able to invoke methods of object a as below

a.xxx()

General speaking final definition is different for object and basic variable.

Solution 3

final modifier means that you cannot change variable, i.e. reference to your object, i.e. assign other value to the same variable. You can however change the state of object itself.

Solution 4

The 'final' refers to the variable sb which is a reference to a StringBuffer. Because it is final, you cannot get sb to refer to another StringBuffer. You can however (as you have found) change the StringBuffer object that sb is currently referring to. Try compiling this code

final StringBuffer sb = new StringBuffer();  
sb.append("abc");  
sb.append("xyz");  
sb = new StringBuffer();  

Solution 5

final variable/reference means, you can't reassign another value to that reference, but, you can change the state of the object.

Here, you are changing the StringBuffer object s internal state

Share:
48,375
Eagle
Author by

Eagle

M a newbie in java. A lot of things in java always increase my quest to dive in more..

Updated on November 08, 2020

Comments

  • Eagle
    Eagle over 3 years

    Following is a code where I have tried to change the value of a final variable. First it prints "a" then "ab". So, if we can change the value of a final variable then what's the benefit of declaring a variable as final? what is use of final key word then? Please any one can help me with this?????

    package test_new;
    public class FinalVarValueChange {
        public static void main(String[] args){
           final StringBuffer s=new StringBuffer("a");
           System.out.println("s before appending  :"+s);
           s.append("b");
          System.out.println("s after appending  :"+s);     
       }
    }
    
  • Maheshbabu Jammula
    Maheshbabu Jammula over 10 years
    yes .we cant reassign ,not refernce
  • Eagle
    Eagle over 10 years
    Thanks folks for helping me... :)
  • Eagle
    Eagle over 10 years
    Thanks Lawrence for detailed description...
  • chacham15
    chacham15 about 4 years
    This isnt exactly true. final and const ARE the same, the main difference here is that the pointer in Java is hidden from you. I.e. final StringBuffer s is the same as StringBuffer * const s (not to be confused with const StringBuffer *s which is different.)