The Artima Developer Community
Sponsored Link •

Java Answers Forum
How can I create an immutable class?

4 replies on 1 page. Most recent reply: Nov 18, 2004 4:12 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Amit

Posts: 23
Nickname: amitactsh
Registered: Nov, 2002

How can I create an immutable class? Posted: Nov 17, 2004 12:22 AM
Reply to this message Reply
Advertisement
We all know that java.lang.String is an immutable class ie the state of an string object can't be changed every time we concat a String object or assign some other value a new object is created.
My query is , is there any way such that we could create our own class immutable


Dave Hinton

Posts: 42
Nickname: catbells
Registered: Oct, 2003

Re: How can I create an immutable class? Posted: Nov 17, 2004 1:09 PM
Reply to this message Reply
Declare all the instance variables to be final.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: How can I create an immutable class? Posted: Nov 17, 2004 5:07 PM
Reply to this message Reply
Declaring member variables final will not automatically make your objects immutable. For example, if you have some getX() method that returns a mutable member of your class (final or not), it can be modified.
import java.util.*;
 
public class NotReallyImmutable
{
   private final List stuff = new ArrayList();
 
   public NotReallyImmutable()
   {
      stuff.add("This should be the one and only entry!");
   }
 
   public List getStuff()
   {
      return stuff;
   }
 
   public void printStuff()
   {
      System.out.println("This is the stuff:");
      Iterator iterator = stuff.iterator();
      for( int i = 1; iterator.hasNext(); i++ )
         System.out.println( i + ". " + iterator.next().toString() );
   }
 
   public static void main( String [] args )
   {
      NotReallyImmutable notReallyImmutable = new NotReallyImmutable();
      List stuff = notReallyImmutable.getStuff();
      stuff.add("Oops!   A mutatation!");
 
      notReallyImmutable.printStuff();
   }
}

To make a class immutable, what's really necessary is to provide no means by which it can be changed, other than the constructor. In the above example for instance, if you really wanted it to be immutable, you might return a clone of the internal list, rather than the list itself (of course, if the list could contain other mutable objects, then you have to be even more cautious and do a deep copy or deal with the issue in another way that ensures the original remains unmolested).

Amit

Posts: 23
Nickname: amitactsh
Registered: Nov, 2002

Re: How can I create an immutable class? Posted: Nov 18, 2004 12:42 AM
Reply to this message Reply
Thanks for your replies,
but I am having one doubt that is here in the given example we are not getting concerned with the state of the object. What I need is that the state of object after creation could not be changed in any case if there is any modification in state then a new object should be created and its refrence should be assigned to the variable.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: How can I create an immutable class? Posted: Nov 18, 2004 4:12 AM
Reply to this message Reply
Huh? Isn't that exactly what is implied when the only way you can configure an object is through its constructor?

If it is still not clear to you, maybe you can provide an example that illustrates what it is you are getting at.

Flat View: This topic has 4 replies on 1 page
Topic: help in my project: getting NullPointerException Previous Topic   Next Topic Topic: first timer

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use