• Post Reply Bookmark Topic Watch Topic
  • New Topic

final keyword with StringBuffer  RSS feed

 
Tom Lee
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
The question is regarding final keyword with StringBuffer context. As per Java rule when we declare constants with final keyword we can't change the value of the variable once it's initialized, but incase of StringBuffer why it is not so?? I mean When I say
final StringBuffer sb = new StringBuffer();
sb.append("abc");
sb.append("xyz");
..why jvm doesn't complain as variable is declared final???..

Any help would be appreciated.

Thanks and Regards,
Tom Lee
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!