Immutable Class in Java
In Java there are many immutable class. String class and all wrapper classes are immutable class.
Immutable class objects are instances whose state doesn’t change after it has been initialized. For example, String is an immutable class and once instantiated its value never changes.
To create immutable class in java, you have to do following steps.
- Declared class as final so it can’t be extended.
- Make all fields private so that direct access is not allowed.
- Don’t provide setter methods for variables
- Make all mutable fields final so that it’s value can be assigned only once.
- Initialize all the fields via a constructor performing deep copy.
- Perform cloning of objects in the getter methods to return a copy rather than returning the actual object reference.
Example to create Immutable class
public final class Person {
private final String name;
private final int age;
private final Collection<String> friends;
public Person(String name, int age, Collection<String> friends) {
this.name = name;
this.age = age;
this.friends = new ArrayList(friends);
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public Collection<String> getFriends() {
return Collections.unmodifiableCollection(this.friends);
}
}
Person class is immutable,
- Don’t provide “setter” methods or methods that modify fields or objects referred to by fields. Setter methods are meant to change the state of object and this is what we want to prevent here.
- Make all fields
final
andprivate
. Fields declaredprivate
will not be accessible outside the class and making themfinal
will ensure the even accidentally you can not change them. - Don’t allow subclasses to override methods. The simplest way to do this is to declare the class as
final
. Final classes in java can not be overridden. - Always remember that your instance variables will be either mutable or immutable. Identify them and return new objects with copied content for all mutable objects (object references). Immutable variables (primitive types) can be returned safely without extra effort.
Also, you should memorize following advantages of immutable class. You might need them during interview.
- Immutable class simple to construct, test, and use
- Immutable class are automatically thread-safe and have no synchronization issues
- Immutable class do not need a copy constructor
- Immutable class do not need an implementation of clone
- Immutable class allow hashCode to use lazy initialization, and to cache its return value
- Immutable class do not need to be copied defensively when used as a field
- Immutable class make good
Map
keys andSet
elements (these objects must not change state while in the collection) - Immutable class have their class invariant established once upon construction, and it never needs to be checked again
- Immutable class always have “failure atomicity” (a term used by Joshua Bloch) : if an immutable object throws an exception, it’s never left in an undesirable or indeterminate state.
post a comment