DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Writing DTOs With Java8, Lombok, and Java14+
  • Redefining Java Object Equality
  • Addressing Memory Issues and Optimizing Code for Efficiency: Glide Case
  • Singleton: 6 Ways To Write and Use in Java Programming

Trending

  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Concourse CI/CD Pipeline: Webhook Triggers
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  • Unlocking AI Coding Assistants: Generate Unit Tests
  1. DZone
  2. Coding
  3. Languages
  4. Java Immutable Explained. Immutable Objects With Examples.

Java Immutable Explained. Immutable Objects With Examples.

In this article, find answers about immutability and its best practices. Practical examples demonstrate the advantages of immutability and related tricks.

By 
Dmitry Egorov user avatar
Dmitry Egorov
DZone Core CORE ·
Updated Nov. 15, 21 · Tutorial
Likes (31)
Comment
Save
Tweet
Share
43.2K Views

Join the DZone community and get the full member experience.

Join For Free

What This Article About

Immutability is a broad concept and has many angles. Immutability is a set of practices that potentially might reduce the number of bugs in your application. As a consequence, this subject invokes confusion. Here, we review it from a different perspective. I'll explain the most important immutability aspects in clear language.

Ich Bin Unkomfortabel

Mutability Problems Examples

Before we start, let's review two examples. They are pretty artificial but explain potential problems with mutable code:

Data Is Changed When It Is Passed to a Function

In this example, we pass the logItem object to the printLogItem function. Expecting that it will be just printed, we later find out that the content of our logItem object has been changed.  

Passing Log Item to Print Method

Build Report Became Inconsistent Due Internal Object Change

In this example, we built a report assuming that internal data (Weather object) won't be changed. Later we printed a report and saw that it became inconsistent (due to internal object change).

Report showing inconsistent data during report printing

You Might Say, “What Stupid Examples!”

Parrot face

The provided examples might look extremely silly. Who would write such code? Well, the code looks obvious just because it's small. In real projects with a hundred complex classes, you can miss such problems.

Fixing Provided Examples

The core problem of the mentioned examples is that the object that shouldn't be changed was changed. Now when we identify the problem we can fix it. But how? There are a couple of ways. Let's start with the most obvious one:  

Making Fields Final

First of all, if we forget to put private visibility, then data still would be accessible. To make it immutable, we need to make it final as below:

Private Final String Data

Removing the Setter

The easiest way we keep the same state is by removing the setter. Would it be enough? 

Removing the setter

Passing Clone Instead of Real Instance

If we want to keep old implementation and setter alive we can solve it from 2 sides:

  1. We can pass a clone of the object.
  2. We can clone passed objects inside printLogItem function.

Clone passed objects

Immutable Primitives in Java

By default in Java, there are a couple of primitives such as int, long, and char. All primitives can't change their value unless you reassign them. Therefore, having them marked as final will make them perfectly immutable. Mark final to make immutable

Immutable String in Java

String in Java is the same as all other primitives. Once it's created it can't change its value.  In order to make it immutable, it's enough to make it final. Easy peasy. Final String

Immutable Class in Java

Class in Java is nothing but a template. Once any class has an implementation, it becomes an Object. Therefore, saying "Immutable Class" is not precisely correct. But in general, it means that all instances of this Class would be immutable. 

Okay, so what are the criteria of Class Immutability?  

  • Each field of this Class has to be immutable.
  • Each field and Class has to be immutable as well.

Making Immutable Java Class

According to the previously explained rules, we can make a simple Immutable Class:

Immutable Parent

Immutable Collections in Java

Collections are also Objects, so if they follow the explained requirements, they are immutable. In practice, it means they are initialized only once and forever. Any changes are disabled or will face exceptions thrown. 

In the next example, I'll show how to make an immutable collection using public Java API (java.util.Collections.*):

Immutable Collection Initialization

Using Lombok for Generating Immutable Code

Lombok is a framework that provides annotation that allows you to generate Java classes using annotations. Lombok provides options for immutable classes (but you have to take care of all immutability requirements: e.g. all internal fields also have to be immutable).

Lombok @Value Annotation: Immutable Boilerplate Bean

To quickly generate immutable Java Bean with final fields and hashcode/equals, you need to use @Value annotation:@Value annotation

Also, @Value annotation provides an option with static factory method:Static Factory Method

Is Singleton Immutable? 

No. Singleton pattern defines that for a given class, only one instance can exist. If this class follows all immutability requirements, it can be immutable.

Is Enum Immutable? 

This question is pretty similar to the previous one. Enum is a class that has a pre-defined number of instances. Inside each enum, we might have mutable data, but if internal fields of enum are immutable, then enum is immutable as well.

Main Advantages of Immutability Practices

Among all existent positive sides of immutability there are two main benefits:

  • Application becomes more predictable and won't face inconsistency (for immutable data).
  • Immutable parts of the application will be Thread Safe (IT'S VERY VALUABLE!).

Conclusion

The described practice is not mandatory, and you still can write reliable applications without immutable tricks. However, the explained recommendations are not just a "good tone," but a practical way to make stable and less buggy applications. In an enterprise world with huge applications, its practice should be mandatory.  Thanks for reading!

Java (programming language) Object (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Writing DTOs With Java8, Lombok, and Java14+
  • Redefining Java Object Equality
  • Addressing Memory Issues and Optimizing Code for Efficiency: Glide Case
  • Singleton: 6 Ways To Write and Use in Java Programming

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!