aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Immutable classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Immutable classes" Watch "Immutable classes" New topic
Author

Immutable classes

kathir vel
Ranch Hand

Joined: Jul 03, 2007
Posts: 92
Hi All,
I saw in JavaRanch tips, "Watch for Math class being an option for immutable classes". What this means?Can anyone clarify me?Thanks.
victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247
Immutable = final;

String object are immutable.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

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.

Kaydell
[ July 26, 2007: Message edited by: Kaydell Leavitt ]
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Immutable = final;


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.

Kaydell
[ July 26, 2007: Message edited by: Kaydell Leavitt ]
kathir vel
Ranch Hand

Joined: Jul 03, 2007
Posts: 92
Thank You for your replies.So Math class cann't be instantiated because there is no constructor.Am I right?
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

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.

Kaydell
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Kaydell Leavitt:
I think that this is all that you need to do to make your own immutable type.
You should make the class final, so nobody can subclass it and break the immutable behaviour.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

You should make the class final, so nobody can subclass it and break the immutable behaviour.


That's a good point. That helps me to understand why immutable classes such as String and the primitive wrapper classes are final.

Kaydell
[ July 27, 2007: Message edited by: Kaydell Leavitt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Immutable classes