How to create immutable class in Java

0 votes
Could you provide a comprehensive guide on the steps and principles for designing and implementing an immutable class in the Java programming language?
Oct 13, 2023 in Java by Saniya
• 3,360 points
442 views

1 answer to this question.

0 votes

To create an immutable class in Java, follow these steps and principles:

1. Make the class `final`:

Declare your class with the `final` keyword. This prevents the class from being extended (subclassed), which is necessary to ensure immutability.

```java
public final class ImmutableClass {
    // ...
}
```

2. Declare all fields as `private` and `final`:

All fields of the immutable class should be declared as `private` and `final`. This restricts direct access and modification to the fields.

```java
public final class ImmutableClass {
    private final int intValue;
    private final String stringValue;
    // ...
}
```

3. Provide a constructor to initialize all fields:

Create a constructor that accepts parameters for initializing all fields of the class. Assign these parameters to the `final` fields in the constructor.

```java
public ImmutableClass(int intValue, String stringValue) {
    this.intValue = intValue;
    this.stringValue = stringValue;
}
```

4. Avoid exposing mutable objects:

If your class contains mutable objects (e.g., collections or arrays), make sure to return a copy of them or provide access methods that return copies, not the actual objects. This prevents clients from modifying the internal state.

```java
public List<String> getStringList() {
    // Return a copy of the internal list to maintain immutability.
    return new ArrayList<>(stringList);
}
```

5. Don't provide setters:

Remove any setter methods from the class, as immutability means objects cannot be modified after creation.

6. Avoid modifying internal state:

Ensure that none of the methods within the class modify the state of the object. All methods should be side-effect-free.

7. Provide accessors for all fields:

Create accessor methods (getters) for accessing the values of the `final` fields.

```java
public int getIntValue() {
    return intValue;
}

public String getStringValue() {
    return stringValue;
}
```

8. Implement `equals()` and `hashCode()` methods:

Override the `equals()` and `hashCode()` methods to ensure proper comparison and hashing of objects based on their values.

9. Make the class Serializable (optional):

If your class needs to be serialized, implement the `Serializable` interface. Ensure that the fields are also `Serializable` or marked as `transient` if needed.

10. Document the immutability:

Clearly document that your class is immutable in the class's Javadoc comments. This helps other developers understand and respect the immutability contract.

11. Consider creating a builder (optional):

If your class has many fields, you may consider creating a builder class to simplify the object creation process.

12. Thread safety:

Immutability inherently provides a degree of thread safety. However, ensure that there are no race conditions in methods, and synchronize if necessary.

Here's a complete example of an immutable class:

```java
public final class ImmutableClass {
    private final int intValue;
    private final String stringValue;
    private final List<String> stringList;

    public ImmutableClass(int intValue, String stringValue, List<String> stringList) {
        this.intValue = intValue;
        this.stringValue = stringValue;
        this.stringList = new ArrayList<>(stringList); // Make a defensive copy
    }

    public int getIntValue() {
        return intValue;
    }

    public String getStringValue() {
        return stringValue;
    }

    public List<String> getStringList() {
        return new ArrayList<>(stringList); // Return a copy to maintain immutability
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ImmutableClass that = (ImmutableClass) o;
        return intValue == that.intValue && Objects.equals(stringValue, that.stringValue);
    }

    @Override
    public int hashCode() {
        return Objects.hash(intValue, stringValue);
    }
}
``
answered Oct 16, 2023 by anonymous
• 3,360 points

edited Oct 19, 2023 by anonymous

Related Questions In Java

0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,140 points
1,543 views
0 votes
1 answer
0 votes
1 answer

How to create a temporary directory/folder in java?

If you are using JDK 7 use ...READ MORE

answered Aug 28, 2018 in Java by Frankie
• 9,830 points
6,396 views
0 votes
1 answer

How to use nested class in Java?

This program will help you understand the ...READ MORE

answered Mar 3, 2019 in Java by Priyaj
• 58,020 points
542 views
0 votes
1 answer

How to create database in MySql using Java?

First, you will have to create connection ...READ MORE

answered Aug 27, 2019 in Java by Juni
760 views
0 votes
1 answer

How to create text labels in Swing, Java?

In the frame, you can use the ...READ MORE

answered Aug 27, 2019 in Java by Jimmy
1,100 views
+1 vote
1 answer

How to create database in MySql using Java?

First, you will have to create connection ...READ MORE

answered Sep 30, 2019 in Java by Shri
3,292 views
0 votes
1 answer

How to create a memory leak in Java?

Hello @kartik, A simple thing to do is ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,840 points
728 views
0 votes
1 answer

How many ways to create object in java?

In Java, there are several methodologies to ...READ MORE

answered Nov 9, 2023 in Java by anonymous
• 3,360 points
409 views
0 votes
1 answer

How to take character input in Java?

In Java, you can take character input ...READ MORE

answered Oct 19, 2023 in Java by anonymous
• 3,360 points

edited Oct 19, 2023 by anonymous 700 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP