Inquisitive
Inquisitive

Reputation: 7856

Usefulness of immutable objects when the state of a program constantly changes

I know that immutable objects always have the same state, the state in which they are actually created. Their invariants are establised by the constructor and since their state does not change after construction , those invariants always hold good and this is why they are safe to publish in a multi threaded environment. This is all fine but since we live in a dynamic world , where the state of program changes continuously , what benefits do such objects give us if we construct the state of our program through immutable objects?

Upvotes: 1

Views: 526

Answers (4)

RalphChapin
RalphChapin

Reputation: 3158

Immutable objects are really useful in cases like this, with a String object:

public class A  {
    private volatile String  currentName = "The First Name";

    public String getCurrentName()  {
        // Fast:  no synching or blocking!  Can be called billions of times by
        // billions of threads with no trouble.
        // (Does need to be read from memory always because it's volatile.)
        return currentName;
    }
    public whatever someMethod()  {
        ... code ...
        // Simple assignment in this case.  Could involve synchronization
        // and lots of calculations, but it's called a lot less than
        // getCurrentName().
        currentName = newName;
        ... code ...
    }
}
public class B  {
        ... in some method ...
        A  objA = something;
        // Gets "name" fast despite a billion other threads doing the same thing.
        String  name = objA.getCurrentName();
        // From this point on, String referenced by "name" won't change
        // regardless of how many times A.currentName changes.
        ... code with frequent references to objA
}

This allows complex data (or even simple data, this case) that must be consistent (if not precisely up-to-date) to be updated and delivered to anybody who wants it very quickly and in a thread-safe manner. The data delivered will soon become outdated, perhaps, but it will keep its value during the calling method and remain consistent.

Upvotes: 0

Jan Wrobel
Jan Wrobel

Reputation: 7109

Immutable objects allow you to cleanly communicate changes of state to different threads.

It is a good practice to use immutable objects to represent messages exchanged between threads. Once such message is sent, its payload can not be altered, which prevents many concurrency related bugs. If a thread needs to communicate some further changes, it just sends next messages.

Upvotes: 2

amicngh
amicngh

Reputation: 7899

Immutable objects are very helpful when you need some static object whose state never changes .Greatest advantage are immutability , object semantics and smart pointers renders object ownership a moot point. Implicitly this also means deterministic behaviour in the presence of concurrency.

Java has already defined some Immutable classes like String Integer.

Other benefit is they always have "failure atomicity" (a term used by Joshua Bloch) : if an immutable object throws an exception, it's never left in an undesirable or indeterminate state .

Let say if you have a global cache of static objects like country codes , here you can apply Immutability.

Why do we need immutable class?

Upvotes: 0

assylias
assylias

Reputation: 328913

"what benefits do such objects give us" you already answered that.

Regarding the "dynamic" part of your question, if you need to "change" an immutable object, you can create a new one from the old one:

Immutable oldObj = new Immutable(...);
Immutable newObj = new Immutable(oldObj.property1, "a new value for property 2");

If you find that you keep doing that repeatedly, then maybe you need to make the object mutable and add the relevant tread-safety features that are needed to be able to use that object in a concurrent environment.

Upvotes: 3

Related Questions