• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How to make Date Immutable

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Christophe, Jesper, Maneesh for your responses

Thanks and Regards
Vijay
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
  •  
    I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic