MyJavaCode.com

Tutorial and How to Guide on Java Programming. Topics covered include Java Programming Language - Core Java, Spring, Webservices, REST, Hibernate, Maven and Microservices.

  • Core Java
  • REST API’s
  • Spring boot
  • Hibernate
  • Interview Questions
  • Contact Us
  • Projects
  • Offerings

How to Create Immutable Objects in Java

January 13, 2025 By Prasanna Manjunatha

 

In Java programming, creating immutable objects is quite important, particularly in multi-threaded applications where data consistency and thread safety are paramount. Immutable objects, once created, cannot be altered. This immutability can lead to simpler, more robust code that is easier to understand and maintain. In this blog post, we will explore the concept of immutability in Java, discuss its benefits, and will provide you the steps to create an immutable class.

What is Immutability?

Immutability refers to the state of an object remaining constant after its creation. Immutable objects do not allow any modification of their state once they are constructed. This is particularly useful in concurrent programming, as it eliminates issues related to data corruption or synchronization.

Benefits of Immutable Objects

  1. Thread Safety: Immutable objects are naturally thread-safe since their state cannot change after construction, eliminating the need for synchronization. This makes it easier to share the object between multiple threads.
  2. Cache-Friendly: The unchangeable nature of immutable objects makes them ideal candidates for caching, as their hash code remains constant.
  3. Enhanced Security: Immutable objects provide secure data access, especially important in scenarios where data should not be altered once constructed.

Also read: How to configure JaCoCo for Code Coverage in Spring boot Applications

How to Create Immutable Objects in Java

Creating an immutable class in Java involves a few key principles and techniques:

1. Use final Class Modifier

Mark the class with the final keyword to prevent subclassing, which can potentially alter its immutable behavior.

public final class ContactInfo {
    ...
}

2. Declare All Fields as private and final

Ensure that fields are private to prevent access from outside the class. Also, fields should be declared final so they can be assigned only once.

private final String name;
private final String email;

3. No Setter Methods

Do not provide “setter” methods that modify fields or objects referred to by fields.

4. Initialization via Constructor

All fields should be initialized exclusively via the constructor. This ensures the object’s state is set when it is created.

public ContactInfo(String name, String email) {
    this.name = name;
    this.email = email;
}

Also Read: Top 5 Code Coverage Tools for Java: A Comprehensive Guide for 2024

5. Deep Copies for Mutable Objects

If your class must store references to mutable objects, ensure that these objects are not changeable from outside code. This is typically achieved by creating deep copies of objects both on construction and on return by getters.

private final Date birthDate;

public ContactInfo(Date birthDate) {
    this.birthDate = new Date(birthDate.getTime()); // Create a new Date object
}

public Date getBirthDate() {
    return new Date(birthDate.getTime()); // Return a copy, not the real object
}

6. Use Immutable Members

Whenever possible, use immutable objects for fields. For example, use String or boxed primitive types like Integer.

Also Read: Externalizing MyBatis SQL Queries in a Spring Boot Application

Example: Creating an Immutable Class

import java.util.Date;

public final class Employee {
    private final int id;
    private final String name;
    private final Date birthDate; // Using Date which is mutable and needs careful handling

    /**
     * Constructs an Employee instance with provided details.
     * Ensures the birthDate is handled immutably by creating a defensive copy.
     *
     * @param id the employee's ID
     * @param name the employee's name
     * @param birthDate the employee's birth date
     */
    public Employee(int id, String name, Date birthDate) {
        this.id = id;
        this.name = name;
        // Make a defensive copy of mutable objects to ensure the immutability of Employee
        this.birthDate = new Date(birthDate.getTime());
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    /**
     * Returns a defensive copy of the birthDate to ensure the caller cannot alter the internal state.
     * 
     * @return a new Date object representing the employee's birth date
     */
    public Date getBirthDate() {
        return new Date(this.birthDate.getTime());
    }
}

 

Conclusion

Creating immutable objects in Java is a powerful technique for achieving simplicity and reliability in applications, especially those dealing with concurrency. By following the principles outlined in this guide, developers can ensure that their objects remain constant, secure, and thread-safe throughout their lifecycle.

Immutability might introduce some design complexities, such as the need for copying objects or performing defensive copies, but the benefits often outweigh these costs in critical applications. As you design more systems, you’ll find that immutability can greatly enhance both performance and robustness.

email
print

About Prasanna Manjunatha

Prasanna is a Toronto based Java Consultant with more than 15 years of Software Development experience. His areas of expertise include Core Java, Spring boot, REST, Microservices, Hibernate, NoSQL, Docker, Kubernetes and AWS.

Currently Trending

  • How to Extract Private Key from Java Keystore (.JKS) or .P12 File
  • Getting Started with Spring Boot: A Spring Boot Setup Tutorial
  • How to Solve Maven invalid CEN header error
  • How to configure JaCoCo for Code Coverage in Spring boot Applications
  • Top 5 Java Code Coverage Tools
  • How to Update MySQL Collation for all tables in the schema
  • How to Create Immutable Objects in Java
  • How to Deploy Exploded WAR file in JBoss EAP Server
  • How to check whether a Collection is Null or Empty in Java
  • Five minutes guide for creating SOAP web service in Java using Axis2

Connect with us

  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

TAG CLOUD

Axis2 Code Coverage CodeCoverage cron expression Data Structure Frameworks Hashing HashMap Hibernate IntelliJ java Java Mail jboss jpa 2.1 Maven MyBatis MySQL PDF Quartz REST REST API SOAP Springboot Spring boot SQL Tools Tutorial web service

All time Popular Posts

  • How to Extract Private Key from Java Keystore (.JKS) or .P12 File
  • How to Solve Maven invalid CEN header error
  • How to check whether a Collection is Null or Empty in Java
  • How to Generate MD5 Hash in Java
  • How to configure JaCoCo for Code Coverage in Spring boot Applications
  • Automate Audit Fields in Spring Boot with MyBatis: A Generic Solution for CRUD Operations
  • How to Update MySQL Collation for all tables in the schema
  • How to Password Protect a PDF File using Java
  • How to Deploy Exploded WAR file in JBoss EAP Server
  • Java API to get Domain Authority and other Moz URL Metrics

Copyright © 2026 MyJavaCode.com