my dog learned polymorphism*
The moose likes Java in General and the fly likes final static StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "final static StringBuffer" Watch "final static StringBuffer" New topic
Author

final static StringBuffer

Sunil Manheri
Ranch Hand

Joined: Jul 07, 2004
Posts: 32
Hi there...


The output is: Hello world!!
Why?
[ November 01, 2004: Message edited by: Sunil Manheri ]

TechMindViews - Technical Notes
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Because the "final" and "static" modifiers apply to the the behaviour of the reference, not the value of the Object that reference points to. So you won't be able to do this:

but you can adjust the object's value all you like, as you do with append(). If you want the value of an object not to be changeable you must use an immutable object (in this case a String is a perfect fit).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sunil Manheri
Ranch Hand

Joined: Jul 07, 2004
Posts: 32
Hi Paul,
Thanx for the reply. Got the point, but not clear...
can u explain it in detail please?
Regards,
Sunil.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

So, keep in mind that final means you can't change what object this variable points to. However you can change the state of the object it refers to. In other words: its a reference variable. -- you can't change what the variable refers to. You can only change the state of that object

So... check out this code snippet:



does that help?

Also, this isn't an advanced question, so I'm going to move it to the intemediate forum. So please post your replies there. Thanks!


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Sunil Manheri
Ranch Hand

Joined: Jul 07, 2004
Posts: 32
yes...
now i got it.
thanx alot.
regards,
Sunil.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
If you come from C++, this behavior can be somewhat confusing. Even though I first thought that final in Java was the same as const in C++, I eventually learned otherwise. While they are similar, and you can in fact get these same results by using const in C++, this is not quite what I expected. I guess I'm just trying to say that I understand the confusion here. I hope you are enjoying your Java endeavors.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: final static StringBuffer