Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final static StringBuffer

 
Sunil Manheri
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there...


The output is: Hello world!!
Why?
[ November 01, 2004: Message edited by: Sunil Manheri ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Sunil Manheri
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
Thanx for the reply. Got the point, but not clear...
can u explain it in detail please?
Regards,
Sunil.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Sunil Manheri
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes...
now i got it.
thanx alot.
regards,
Sunil.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic