| Author |
Why is StringBuilder a modifiable interface variable ?
|
Graeme Byers
Ranch Hand
Joined: Apr 16, 2004
Posts: 127
|
|
Why don't StringBuffer and StringBuilder when declared in interfaces behave as expected ?
Thank you.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
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?
|
[Jess in Action][AskingGoodQuestions]
|
 |
Vikas Kapoor
Ranch Hand
Joined: Aug 16, 2007
Posts: 1374
|
|
|
And We all know that STRING IS IMMUTABLE.
|
 |
 |
|
|
subject: Why is StringBuilder a modifiable interface variable ?
|
|
|