File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to make Date Immutable Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to make Date Immutable" Watch "How to make Date Immutable" New topic
Author

How to make Date Immutable

Vijay jai Singh
Greenhorn

Joined: Jan 07, 2009
Posts: 26
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

Joined: Nov 24, 2005
Posts: 14672
    
  11

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 ?


[My Blog]
All roads lead to JavaRanch
Vijay jai Singh
Greenhorn

Joined: Jan 07, 2009
Posts: 26
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
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8439

Search the forums.
I remember reading an article on this in the Journal by Illja Preuss if I am not mistaken.


[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8439

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

Joined: Nov 24, 2005
Posts: 14672
    
  11

I hope he is on vacation and doesn't see this

I'll tell him anyway
Vijay jai Singh
Greenhorn

Joined: Jan 07, 2009
Posts: 26
thanks Christophe, Jesper, Maneesh for your responses

Thanks and Regards
Vijay
G.Sathish kumar
Ranch Hand

Joined: Jul 27, 2009
Posts: 84
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).


Thanks
Sathish kumar
SCJP, SCWCD
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
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 agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
     
    subject: How to make Date Immutable
     
    Similar Threads
    Java
    Immutable Class
    How to make a class immutable??????????
    class Immutable
    Why Strings are immutable in java ?