Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does final not work ?

 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class AA
{
final StringBuffer sb = new StringBuffer("I am final");
public AA()
{
}
public final StringBuffer getSb()
{
return this.sb;
}
public static void main(String[] args)
{
A a = new A();
StringBuffer localSB = a.getSb();
localSB.append("....");
System.out.println(localSB);
localSB = new StringBuffer();
System.out.println(localSB.toString());
}
}
The output shows that the final sb variable has been changed and that the final reference gets assigned to another string buffer object.How does this happen?
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

returns "I am Final...."
Also, localSb != localSB. Different case means different object reference.
HTH
[ October 21, 2003: Message edited by: Donald R. Cossitt ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final *is not* const.
final doesn't mean that the object cannot be modified, it only means that the variable can not be changed to reference a different object.
final StringBuffer sb = new StringBuffer();
sb = new StringBuffer(); // compile time error because sb is final
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandra:
Here is how the JLS explains it.

Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain the JLS in the above code what exactly does the method return is it a final object or final reference. If it is a final object the state should be immutable and if it is a final reference the localSB should not be assigned a new object. In either case the code should fail. How if the JLS justified here.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chandra Bairi:
Can you explain the JLS in the above code what exactly does the method return is it a final object or final reference. If it is a final object the state should be immutable and if it is a final reference the localSB should not be assigned a new object. In either case the code should fail. How if the JLS justified here.

The final modifier for a method does *not* apply to the return value, but to the method itself. It simply means that the method cannot be redefined (overwritten) by subclasses:
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chandra Bairi:
If it is a final object the state should be immutable

Java does not have the concept of declaring objects to be final, only references to objects. So therefore, as the JLS states, the state of the object may be changed, but the reference to the object is not changeable.
In java, immutability of objects is done differently. Basically, you create a final class (one that can't be overridden) with private member variables and don't provide accessors to those variables. Then the only way you can set the state is through the constructor, but once the state is set, it cannot be changed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic