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 is StringBuilder a modifiable interface variable ?

 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't StringBuffer and StringBuilder when declared in interfaces behave as expected ?
Thank you.

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has no concept of making a particular instance of a class "unmodifiable." If you use an instance of a modifiable (usually we say "mutable") class as a member of an interface, then it's going to be possible to modify that object.

Note that in your example, you didn't show something like

BUF = new StringBuffer("replacement");

If you tried this, you'd find that because BUF is final, this is illegal. The object that BUF points to can be modified, but BUF, being final, can not.

Make sense?
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And We all know that STRING IS IMMUTABLE.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic