File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why is StringBuilder a modifiable interface variable ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why is StringBuilder a modifiable interface variable ?" Watch "Why is StringBuilder a modifiable interface variable ?" New topic
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: 24187
    
  34

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is StringBuilder a modifiable interface variable ?