Java Tip : Creating Immutable classes

image

We have made this class as immutable:

  • The class is final. That means no subclasses can be created. A subclass of an immutable class can be made mutable again. As noted above, an attacker could use that to get to confidential data.
  • The variables are all final and cannot be changed after construction
  • In the constructor we use the import org.joda.time.DateTime class. This is a better version than the java.util.Date because it is immutable. Using a java.util.Date would be dangerous as it is a mutable class and we can’t control the calling thread (which might modify it).
  • There are no setter methods for the members.

One thought on “Java Tip : Creating Immutable classes

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.