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.
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.
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?
SCJP 1.4, SCWCD in process
Joined: Aug 03, 2002
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.