I saw in JavaRanch tips, "Watch for Math class being an option for immutable classes"
The Math class is not only immutable, you can't even create an instance of it, so you can't change any instance variables because there are none.
I agree that a better example of an immutable class is the String class (oh, but the question was about tricky questions).
You make classes immutable when you want them to be shared and you don't have to worry that they will be changed in one context which would change the object everywhere that it is referenced because immutable objects can't be changed at all.
I'd like to clarify that when you use the final keyword on an object reference variable that this doesn't make the object that is referenced immutable.
To make your own object immutable, you have to do the following:
1. In your constructor for your own immutable object, primitives and other immutable objects can be accepted without any further work on your part. 2. In your constructor for your own immutable object, if you accept non-immutable objects, you have to make copies of them to assure that the caller who called your constructor doesn't still have a reference that might be used to change the non-immutable object that is now a field of your object. 3. All of the fields of your immutable object must be private. 4. You can't have any setter() methods for any of your fields. 5. You can have getter() methods for primitives and for immutable fields. 6. For non-immutable fields, if you have any getters, you must return a copy of the field so that the caller gets a reference to their own copy and can't possibly change your copy.
I think that this is all that you need to do to make your own immutable type.
Math class cann't be instantiated because there is no constructor.Am I right?
Every class has at least one constructor.
If you don't write a constructor, the compiler will create a default constructor that takes no parameters and calls the inherited constructor. To understand why this happens, you have to understand that Java assures that at least one constructor will be called for every class and that the inherited constructor will be called. If a class can be instantiated, you can't keep constructors from being called. Every class has at least one constructor and at least one constructor will be called in each class. This happens for abstract classes, classes with no instance variables, etc. Every class. Even the Math class must have a constructor.
The Math class uses a trick to make it non-instantiable. A constructor is defined so that the compiler won't automatically define one and automatically invoke it. Then the Math class's constructor is made private so that it can't be called at all. This satisfies the compiiler that there is a constructor, but making this constructor private makes it un-callable so you can't construct the Math class since the private constructor isn't defined outside of the Math class.
This is how the Math class is made un-instantiable.