Immutable Definition & Meaning

In object-oriented and functional programming, an immutable object is an object whose state cannot be changed after it is created. The public API of an immutable object guarantees that it will behave in the same way during its lifetime. In some cases, an object is considered immutable even if internally-used attributes change but the object’s state appears unchanged from an external point of view. Below is an example of a basic immutable class:

class User {

  private final Long id;
  private final String name;

  User(Long id, String name) {
    this.id = id;
    this.name = name;
  }

}

All fields are final, meaning that the compiler is told that their values must not change once initialized. All field values are then passed into the constructor.

Immutable object benefits

Since the object is unchangeable, users know exactly what to expect from it. Code cannot be changed, meaning that there’s no opportunity to introduce inconsistencies that may lead to runtime errors. Immutable objects are thread-safe, so synchronization issues are avoided. They are easier to design, implement, and use than mutable classes.

Once an immutable object is created and verified, no other thread or background process will be able to change the object without a user’s direct knowledge. This is useful for programs that need high security. When an issue arises, debugging is easier with immutable objects because a bug’s origin can be easily traced.

Immutable vs. mutable objects

While immutable objects cannot change their state, mutable objects can. Mutable objects provide methods to change an object’s content and are not as thread-safe as immutable objects.

In Java, examples of mutable objects include StringBuilder and java.util.Date. Examples of immutable objects include all legacy classes, wrapper classes, and string classes.

In Python, examples of mutable types include list, dict, and set. Immutable types in Python include int, float, bool, string, unicode, and tuple.






Abby Dykes
Abby Dykes
Abby Dykes is a writer and editor for websites such as TechnologyAdvice.com, Webopedia.com, and Project-Management.com, where she covers technology trends and enterprise and SMB project management platforms. When she’s not writing about technology, she enjoys giving too many treats to her dog and coaching part-time at her local gym.

Top Articles

The Complete List of 1500+ Common Text Abbreviations & Acronyms

UPDATED: This article was updated April 6, 2021 by Web Webster   From A3 to ZZZ we list 1,559 text message and online chat abbreviations to...

How to Create a Website Shortcut on Your Desktop

UPDATED: This article was updated April 6, 2021 by Web Webster   This Webopedia guide will show you how to create a desktop shortcut to a...

Windows Operating System History & Versions

The Windows operating system (Windows OS) refers to a family of operating systems developed by Microsoft Corporation. We look at the history of Windows...

What are the 5 Generations of Computers?

UPDATED: This article was updated on April 6, 2021 by Web Webster   Learn about each of the 5 generations of computers and major technology developments...

Hackerspace Definition & Meaning

What is a hackerspace? A hackerspace, also known as a hacklab, incubator, or hackspace,...

Random Access Memory (RAM)...

UPDATED: This article Updated April 6, 2021 by Web Webster   Random Access Memory (RAM)...

OEM – original equipment...

UPDATED: This article was updated April 6, 2021 by Web Webster OEM (pronounced as...