wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to know a class is Immutable or Not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to know a class is Immutable or Not" Watch "How to know a class is Immutable or Not" New topic
Author

How to know a class is Immutable or Not

Madan, Gopal
Ranch Hand

Joined: Aug 13, 2001
Posts: 86
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

Joined: Aug 26, 2001
Posts: 7610
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Madan, Gopal
Ranch Hand

Joined: Aug 13, 2001
Posts: 86
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Sep 23, 2000
Posts: 3286
    
    7
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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Nov 04, 2001
Posts: 16
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Nov 04, 2001
Posts: 16
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Oct 31, 2001
Posts: 14
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

Joined: Aug 26, 2001
Posts: 7610
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
 
 
subject: How to know a class is Immutable or Not