Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Immutable Classes

Thread view: 
GenxLogic - 17 Apr 2006 10:58 GMT
Hi,
  i want to know that how can i convert a mutable class into immutable
class
Thanks in advance.
Deepak
Darryl L. Pierce - 17 Apr 2006 11:48 GMT
>    i want to know that how can i convert a mutable class into immutable
> class

If you're referring to class in the Java standard API, the answer is that
you can't. If you're referring to your own classes, then you can do it by
just added a setter method for the properties you want to make mutable.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"

Brzezi - 17 Apr 2006 12:52 GMT
pon, 17 kwi 2006 o 12:48 GMT, Darryl L. Pierce napisał(a):

>>    i want to know that how can i convert a mutable class into immutable
>> class
> If you're referring to class in the Java standard API, the answer is that
> you can't. If you're referring to your own classes, then you can do it by
> just added a setter method for the properties you want to make mutable.

note that he wrote: "how can i convert a mutable class into immutable"

simple example:

int foo;

foo is mutable

Integer bar = new Integer(foo);

now bar is immutable

Pozdrawiam
    Brzezi
Signature

[    E-mail: brzezi@enter.net.pl ][    Mollison's Bureaucracy Hypothesis:     ]
[       Ekg: #3781111            ][        If an idea can survive a           ]
[ LinuxUser: #249916             ][        bureaucratic review and be         ]
                                 [        implemented                        ]
                                 [        it wasn't worth doing.             ]

Oliver Wong - 17 Apr 2006 15:27 GMT
> If you're referring to class in the Java standard API, the answer is that
> you can't
[make an immutable class mutable.]

   Couldn't you do some trickery like:

<pseudoCode>
public class MutableString [extend String if only it weren't final] {
 private String internalState;
 public String getValue() {
   return internalState;
 }
 public void setValue(String newValue) {
   this.internalState = newValue;
 }
}
</pseudoCode>

   Basically, using the decorator pattern to add mutability to a class?

   - Oliver
Thomas Hawtin - 17 Apr 2006 14:46 GMT
>> If you're referring to class in the Java standard API, the answer is that
>> you can't
[quoted text clipped - 4 lines]
> <pseudoCode>
> public class MutableString [extend String if only it weren't final] {

If you have a reference to an immutable object, you expect it to remain
immutable. That sort of thing is a bit of a hack, although you could
have an interface/abstract class that explicitly claim to be not
necessarily immutable.

You can do something like adding mutable variables to immutables:

public abstract class Ex {
    private Ex() {
        throw new Error();
    }
    private static final Map<MyImmutable,String> text =
        new WeakHashMap<MyImmutable,String>();
    public static synchronized void setText(
        MyImmutable target, String text
    ) {
        text.put(target, text);
    }
    public static synchronized String getText(
        MyImmutable target
    ) {
        return text.get(target);
    }
    public static synchronized String removeText(
        MyImmutable target
    ) {
        return text.remove(target);
    }
}

Of course if you can have two instances of MyImmutable that are equal it
goes a bit wrong. As does having the value reference the key.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Darryl L. Pierce - 17 Apr 2006 17:07 GMT
>     Couldn't you do some trickery like:
>
> <pseudoCode>
> public class MutableString [extend String if only it weren't final] {

Except that, like you show above, most immutable classes are also final, and
were made immutable for a reason.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"

Mark Thomas - 17 Apr 2006 12:17 GMT
> Hi,
>    i want to know that how can i convert a mutable class into immutable
> class
>  Thanks in advance.
> Deepak

You need to remove or replace all methods that change the state of the
object (except the constructors).  If a method is needed which has to
change the state of the object, modify it so that it returns a copy of
the original object modified as required.  The String class is a good
example of an immutable class.

Mark
Darryl L. Pierce - 17 Apr 2006 12:41 GMT
> You need to remove or replace all methods that change the state of the
> object (except the constructors).  

Ack! I misread the original post and thought he was asking how to make an
immutable class mutable... :-/

Signature

Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"

Matthias Kaeppler - 17 Apr 2006 15:57 GMT
> Hi,
>    i want to know that how can i convert a mutable class into immutable
> class

Assuring immutability is not a trivial task, as there are many
subtleties which can breach immutability of an object or class. I
suggest you read up on immutability in more detail (I suppose Google
will turn up with papers on the subject), as it isn't a topic which can
be covered with a few sentences.

Good luck :)

Regards,
Matthias
Roedy Green - 17 Apr 2006 20:12 GMT
On 17 Apr 2006 02:58:14 -0700, "GenxLogic"
<Deepak_Kumar09@infosys.com> wrote, quoted or indirectly quoted
someone who said :

>Hi,
>   i want to know that how can i convert a mutable class into immutable

It is much easier to go the other way.

You could reimplement any mutator methods to throw an exception.

You could use a copy constructor that copies the field data into a new
immutable object unrelated to the original.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

zlatozar - 19 Apr 2006 09:34 GMT
1. Don't provide any methods that modify the object (known as
mutators).
2. Ensure that no methods may be overridden.
3. Make all fields final.
4. Make all fields private.
5. Ensure exclusive access to any mutable components.

See "Effective Java"


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.