0% found this document useful (0 votes)
28 views

Immutable Object

The document discusses immutable objects in Java. An immutable object is an object whose state cannot be modified after it is created. A String in Java is immutable - methods like toLowerCase() do not modify the original String, but instead return a new String object. To make a String reference the lowercase version, the original variable must be reassigned. The document also provides an example of a mutable Cart class, and how to make it immutable by returning an unmodifiable list from getItems().

Uploaded by

Saeed Mirzaei
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Immutable Object

The document discusses immutable objects in Java. An immutable object is an object whose state cannot be modified after it is created. A String in Java is immutable - methods like toLowerCase() do not modify the original String, but instead return a new String object. To make a String reference the lowercase version, the original variable must be reassigned. The document also provides an example of a mutable Cart class, and how to make it immutable by returning an unmodifiable list from getItems().

Uploaded by

Saeed Mirzaei
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

immutable object

A classic example of an immutable object is an instance of the Java String


class.

String s = "ABC";
s.toLowerCase();

The method toLowerCase() will not change the data "ABC" that s contains.
Instead, a new String object is instantiated and given the data "abc" during its
construction. A reference to this String object is returned by the toLowerCase()
method. To make the String s contain the data "abc", a different approach is
needed.

s = s.toLowerCase();

Now the String s references a new String object that contains "abc". The String
class's methods never affect the data that a String object contains.

For an object to be immutable, there has to be no way to change fields,


mutable or not, and to access fields that are mutable. Here is an example of
a mutable object written in the Java programming language.

class Cart {
private final List items;

public Cart(List items) { this.items = items; }

public List getItems() { return items; }


public int total() { /* return sum of the prices */ }
}

An instance of this class is not immutable: one can add or remove items either
by obtaining the field items by calling getItems() or by retaining a reference
to the List object passed when an object of this class is created. The following
change partially solves this problem. In the ImmutableCart class, the list is
immutable: you cannot add or remove items. However, there is no guarantee
that the items are also immutable. One solution is to use the decorator pattern
as a wrapper around each of the list's items to make them also immutable.
class ImmutableCart {
private final List items;

public ImmutableCart(List items) {


this.items = Collections.unmodifiableList(new ArrayList(items));
}

public List getItems() {


return items;
}
public int total() { /* return sum of the prices */ }
}

You might also like