Immutable Object

noun

say it: “ih-MYOO-tuh-bul OB-jekt” /ɪˈmjuːtəbəl ˈɑbdʒɛkt/

An object whose values can't be changed after it is made; to change something, you make a new copy.

What is Immutable Object?

An immutable object cannot change after it is created. To "change" it, you make a new object with the new values. Strings in C#, Java and Python work this way.

public record Point(int X, int Y);

var a = new Point(1, 2);
var b = a with { X = 5 }; // a is unchanged; b is a new Point(5, 2)

In a sentence

  • “Sam's first shipment was an immutable object, so adding his signature made a new copy and left the printed label's shipment alone.”

Level: Journeyman · More OOP terms at this level

Updated