File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to easily tell if class is immutable 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 easily tell if class is immutable" Watch "How to easily tell if class is immutable" New topic

How to easily tell if class is immutable

Bill Nelsen

Joined: Aug 11, 2004
Posts: 27
A sample test question asked the following:

Which of the following java classes are immutable??

Answer: B, D

I also answered A, because I thought that it doesn't have any mutators and missed the question. Since the API doesn't explicitly state whether a class is immutable, I am looking for a simple test to run to determine mutability.
Stefan Zoerner
Ranch Hand

Joined: Nov 29, 2001
Posts: 121

Just an idea: Not the classes are immutable, but their objects. It means that you cannot change the state of an object after its constructions.

You cannot create Math objects, because the class java.lang.Math does not have any public constructor. Try

It will not compile. Because their are no Math-objects, there are no immutable objects.

Greetings from Hamburg,

Stefan Zoerner
Author of German LDAP-Book
Committer at Apache Directory Project
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Strictly speaking an immutable CLASS is a class defined as final (so that IS explicitly stated in the source).
A class whose INSTANCES are immutable has no setters for any of its fields and generally has those fields declared as final as well so that too is visible in the source.

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Take a look at Math.random and ask once again whether Math has any state.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I think that when it comes to java.lang.Math, John's reasoning is almost metaphysical.

If there are no Math instances, we can't change a Math instance, so Math as a class is immutable.

If ~p, p->q is always true.

The best answer on these classes is that the folks who did the hard work of designing and implementing Java got to define words like "immutable" any way they wanted. As Humpty Dumpty told Alice in Wonderland, "When I use a word, it means just what I choose it to mean --neither more nor less". This quote will not be on the exam but will serve you well as a programmer trying to understand software doc.

With the wrapper classes, it's pretty clear from the API documentation that there is no method that will change the value of an instance.

On the other hand, strings have lots of apparent mutator methods. The creation of new objects whenever you try to change a string is a language feature you just have to learn.

Question: What are the other immutable classes?

Mike Gershman
SCJP 1.4, SCWCD in process
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Barry Gaunt:
Take a look at Math.random and ask once again whether Math has any state.

The reason I threw this one into the pot is because of the fact that the first call to Math.random creates a random number generator object. This has to be referenced by the Math class so that subsequent calls to Math.random generate a new random number. So you could say that the Math class has state which changes and a call to Math.random is what changes the state. There are indeed no instances of the Math class, but the Math class must have a static class variable which does change. Perhaps this is why Math was not one of the options in the above mock question.
I agree. Here's the link:
subject: How to easily tell if class is immutable
It's not a secret anymore!