Immutable Classes

G

GenxLogic

Hi,
i want to know that how can i convert a mutable class into immutable
class
Thanks in advance.
Deepak
 
D

Darryl L. Pierce

GenxLogic said:
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.
 
M

Mark Thomas

GenxLogic said:
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
 
D

Darryl L. Pierce

Mark Thomas said:
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... :-/
 
B

Brzezi

pon, 17 kwi 2006 o 12:48 GMT, Darryl L. Pierce napisa³(a):
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
--
[ E-mail: (e-mail address removed) ][ Mollison's Bureaucracy Hypothesis: ]
[ Ekg: #3781111 ][ If an idea can survive a ]
[ LinuxUser: #249916 ][ bureaucratic review and be ]
[ implemented ]
[ it wasn't worth doing. ]
 
T

Thomas Hawtin

Oliver said:
Darryl L. Pierce said:
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] {

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
 
O

Oliver Wong

Darryl L. Pierce said:
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
 
M

Matthias Kaeppler

GenxLogic said:
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
 
D

Darryl L. Pierce

Oliver said:
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.
 
R

Roedy Green

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.
 
Z

zlatozar

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top