Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Immutable classes

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Immutable = final;

String object are immutable.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You for your replies.So Math class cann't be instantiated because there is no constructor.Am I right?
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic