java

What does immutable mean in Java

Immutable means unchangeable. If an object is immutable in Java, it cannot be modified. This article should clarify what that means exactly.
 

Immutability through final

The keyword final changes variables in such a way that they can only be assigned once. So final does not make an object immutable, but rather a variable “no longer reassignable”. With this method constants are defined in Java:

public static final CONSTANT_VALUE = "Hello";

If variables of primitive data types or immutable types are declared as final from the start, these can no longer be changed.

final int fixedInt = 50;
final String fixedString = "Hello";

 

java-mcq-multiple-choice-questions-and-answersJava MCQ – Multiple Choice Questions and Answers – OOPsThis collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java OOPs”.   1. Which of the…Read More
Imutability of objects

Objects are considered to be immutable, i.e. unchangeable, if all fields can only be accessed for reading. One example is the Person class, which has already been used several times:

public class Person{
    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }
}

Objects of the class Person are unchangeable because the individual fields are read-only. However, the class allows to be inherited, which allows to overwrite getters and add setters.

public class ChangeablePerson extends Person {
    private String firstName;
    private String lastName;

    public ChangeablePerson(String firstName, String lastName) {
       super(firstName, lastName);
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}
 
Thus, variables of type Person can be assigned to mutable objects of type ChangeablePerson, although one would assume that Person objects are immutable:

Person p = new ChangeablePerson("Alex", "Foubice");

One way to prevent this is to make the class Person itself final. This way, derivations by other classes are no longer allowed. You cannot inherit from a final class:

public final class Person{
    //Properties and Getter...
}

Attempting to inherit from this class would result in a compiler error.
 

The problem with mutable properties

If individual fields or properties are of a mutable type, the object as a whole is not immutable.
Autoboxing and Autounboxing in JavaAutoboxing and Autounboxing in JavaAutoboxing is the automatic conversion of a primitive data type into its wrapper class. Autounboxing refers to the automatic conversion of a wrapper class into…Read More mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *