It's not a secret anymore!*
The moose likes Java in General and the fly likes Final & Immutable class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Final & Immutable class" Watch "Final & Immutable class" New topic
Author

Final & Immutable class

Rahul Kumar Tiwari
Greenhorn

Joined: Jun 09, 2008
Posts: 14

Some body told me that immutable class (Like String) that mean this class is define with final keyword but i am looking that StringBuffer Class is also a final class and it is mutable class. can any body clear me difference between final and immutable class.

Thanks
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Making a class final doesn't mean that the class itself is final. If a class is immutable, you can't change the state of that class.

In String versus StringBuffer case,


Hope it helps.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Rahul Kumar Tiwari
Greenhorn

Joined: Jun 09, 2008
Posts: 14
Thanks

But please tell me if all final class is not a immutable class then what properties of a final class make it immutable,
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

If you do not provide access to the class's instance variables (no setters), it will be immutable.


[My Blog]
All roads lead to JavaRanch
Rahul Kumar Tiwari
Greenhorn

Joined: Jun 09, 2008
Posts: 14

Thanks You Very Much
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Final & Immutable class