• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to know a class is Immutable or Not

 
Madan, Gopal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to know whether a class is immutable or mutable? I know that String is immutable(because it said so in books). What about Math class?
Can somebody shed some light on this
Thanks
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Math class does not store any state, all of its methods are static which means that it is more like a class providing short-term services.
You can find out if a class is immutable by looking at its API. If you can't find any method with which you could change its internal state (i.e. the value of its member variables and the one inherited from the superclasses) then you may deduce that the class is immutable. That is the class has no "mutator" methods, which means you cannot mutate (change) the state of the object. It may have "accessor" method (to access its internal state) though. Mutator are often method beginning with "set..." and accessor with "get..."
HIH
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Madan, Gopal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Valentine:
I asked that because in VelMurugan's notes, under trips/traps he said and I quote
Math class being an option for immutable classes !!

Exactly what he means by this?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep I read that too.
AFAIK a class can be immutable as long as it keeps a state, immutable meaning that you cannot change its state. Since the Math class does not maintain any state information, you can not call it immutable. Does it make sense ?
Anyone comment on this ?
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3711
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin, you are absolutely correct but you are reading too much into it. Math class is immutable simply because it is final. It is not immutable in the sense that you are thinking about, however, for the purpose of exam, a class that cannot be subclassed is also immutabile. Note that, by making a class final, the "class" becomes immutable. It's objects may still be mutable!
------------------
SCJP2, SCWCD Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
JQPlus - For SCJP2
JWebPlus - For SCWCD
JDevPlus - For SCJD
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right Paul, I guess I did in fact mix class and object, exactly what any good Java programmer is not supposed to do... I'm a little tired I guess... Sorry about that

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Pervez Amin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that is so, then what about StringBuffer. StringBuffer is a final class and that is not immutable.
"A string buffer implements a mutable sequence of characters"
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like Paul said the class is immutable (read final, can't be subclassed) but instances of it are mutable, i.e. you can modify its internal state...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Pervez Amin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, then lets rephrase question. How can you tell if an OBJECT is immutable (meaning contents of object cannot be change).
e.g lets give 4 examples (which of these classes produce immutable objects and WHY?)
1.java.lang.Double
2.java.lang.Math
3.java.lang.StringBuffer
4.java.lang.Boolean
NOTE: Math class can't produce objects because it can't be instantiated because it's constructor is private.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Double object are clearly immutable since after you build one there is no way to change its value by any means, look at the API
2. You can't have Math objects as explained in the note
3. StringBuffer objects are mutable, i.e. you can change the content of the buffer after creating an instance.
4. Boolean is the same as Double
The key here is to look at the API and know it... You can't tell out of nowhere whether a class or object is immutable or not, you have to look at its API and the comments given by the developers who made those class...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
David Eason
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Valentin Crettaz:
You can find out if a class is immutable by looking at its API. If you can't find any method with which you could change its internal state (i.e. the value of its member variables and the one inherited from the superclasses) then you may deduce that the class is immutable. That is the class has no "mutator" methods, which means you cannot mutate (change) the state of the object. It may have "accessor" method (to access its internal state) though. Mutator are often method beginning with "set..." and accessor with "get..."

I asked a similar question in the Beginner's forum today. From the point of view of C++, the String class has a mutator method called <CODE>operator=</CODE> which lets me write:

Presumably the operator= method is hard coded into javac, since Java doesn't allow operator methods. So it seems that the String class does have a mutator method after all, albeit not one exposed to developers, and is therefore mutable.
What am I missing here?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
the code you wrote does not "mutate" the content of immu but just assign a new String instance to immu.

By doing that you haven't change the content of the String originally referenced by immu, you have just made immu reference another String instance, that's all !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic