• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make Date Immutable  RSS feed

 
Vijay jai Singh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Date in java is mutable. Is there any way to make it Immutable other than creating an object with final key word.

Thanks and Regards
Vijay
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to make it Immutable other than creating an object with final key word.

This would not make it immutable anyway. Its content would still be mutable.

You could make a wrapper class, but is is really necessary to go through all that pain ? Why do you want to make it immutable ?
 
Vijay jai Singh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:
Is there any way to make it Immutable other than creating an object with final key word.

This would not make it immutable anyway. Its content would still be mutable.

You could make a wrapper class, but is is really necessary to go through all that pain ? Why do you want to make it immutable ?


Hi Christophe,

Thanks for your reply. It is not my project requirement. Just thinking about how to implement this. Could you please help:
a) Why it would not be immutable if i use final while creating object of Date.
b) How to make wrapper class.

Thanks and Regards
Vijay
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijay jai Singh wrote:a) Why it would not be immutable if i use final while creating object of Date.

Because making a variable final only means that you cannot change the value of the variable itself; you can still change the state of the object that the variable refers to. For example:


b) How to make wrapper class.

Write a class yourself that has the same methods as class Date, except the set...() methods and other methods that might change the state. Put a private member variable of type Date in your class, and forward all calls to the Date object. For example:
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search the forums.
I remember reading an article on this in the Journal by Illja Preuss if I am not mistaken.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:..by Illja Preuss if I am not mistaken.

I was mistaken indeed. It was authored by David O'Meara. (I hope he is on vacation and doesn't see this)

Here you go. http://www.javaranch.com/journal/2003/04/immutable.htm
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope he is on vacation and doesn't see this

I'll tell him anyway
 
Vijay jai Singh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Christophe, Jesper, Maneesh for your responses

Thanks and Regards
Vijay
 
G.Sathish kumar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:
Vijay jai Singh wrote:a) Why it would not be immutable if i use final while creating object of Date.

Because making a variable final only means that you cannot change the value of the variable itself; you can still change the state of the object that the variable refers to. For example:


b) How to make wrapper class.

Write a class yourself that has the same methods as class Date, except the set...() methods and other methods that might change the state. Put a private member variable of type Date in your class, and forward all calls to the Date object. For example:




i feel when the state of the object change there should be new object for the immutable object so wht code which you mentioned i think will not give immutable object when there is situvation like

data+20 days should be new date object so we need to extend the date class and use super to call the parent instance and the result need to be returned as new date object(i have seen that same thing implemented in string class in java).
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is the standard method for new values for immutable classes. Simple exampleNote:
  • Use final modifier to prevent anybody creating mutable subclasses.
  • All "real" fields are final too.
  • No set methods.
  • If there were any mutable reference type fields, they would be copied before being returned by a get method. You could do that with a copy constructor in the field . . . return newFoo(this.foo);
  • No need for copy constructors in immutable objects
  • The hash code (or the return value for toString) can be calculated when required, or at construction time, and cached, if you prefer.
  •  
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!