my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Why does final not work ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does final not work ?" Watch "Why does final not work ?" New topic
Author

Why does final not work ?

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
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?


Thanks,
Shekar
Donald R. Cossitt
buckaroo
Ranch Hand

Joined: Jan 31, 2003
Posts: 401

returns "I am Final...."
Also, localSb != localSB. Different case means different object reference.
HTH
[ October 21, 2003: Message edited by: Donald R. Cossitt ]

doco
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
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.


For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Aug 20, 2001
Posts: 1817

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.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does final not work ?
 
Similar Threads
static variable vs final variable!
StringBuffer
static final variable
String Buffer
Can anybody explain this?