• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to easily tell if class is immutable

 
Bill Nelsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A sample test question asked the following:

Which of the following java classes are immutable??


A.java.lang.Math
B.java.util.String
C.java.util.StringBuffer
D.java.lang.Boolean
E.java.lang.Object

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
Author
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

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
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at Math.random and ask once again whether Math has any state.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic