Immutable

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 Braden
Abby Braden is an award-winning 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

Text Abbreviations reviewed by Web Webster   From A3 to ZZZ we list 1,559 SMS, online chat, and text abbreviations to help you translate and understand...

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

How to Create a Website Shortcut on Your Desktop

Website Shortcut on Your Desktop reviewed by Web Webster   This Webopedia guide will show you how to create a website shortcut on your desktop using...

Generations of Computers (1st to 5th)

Reviewed by Web Webster Learn about each of the 5 generations of computers and major technology developments that have led to the computing devices that...

Telecommunication

Telecommunication refers to telephony and cellular network technology. However, the broader definition includes...

Spoofing

What is spoofing? As it pertains to cybersecurity, spoofing is when a person disguises...

How to Indent in...

Microsoft Word is a graphical word...