Java Tips : What is immutable object? Can you write immutable object?

Immutable classes are Java classes whose objects cannot be modified once created. Any modification in Immutable objects result in new object.

Java Tips

Java Tips

For example is String is immutable in Java. Mostly Immutable are also final in Java, in order to prevent sub class from overriding methods in Java which can compromise Immutability. You can achieve same functionality by making member as non final but private and not modifying them except in constructor.

Does all property of immutable object needs to be final?

Not necessary as stated above you can achieve same functionality by making member as non final but private and not modifying them except in constructor.

Add comment