Forums Register Login

How to easily tell if class is immutable

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Take a look at Math.random and ask once again whether Math has any state.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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.
Always! Wait. Never. Shut up. Look at this tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3423 times.
Similar Threads
About imutable class
Math
Immutable - Mutable
Ques regarding immutable class
'JvalTest' Java2 Certification Mock Exam Applet
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:22:58.