Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Why is Immutability Important in Java Class Design?

DZone's Guide to

Why is Immutability Important in Java Class Design?

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

Let’s revisit another common topic which Java programmers deal with on a daily basis –Immutable objects and classes. One of the most prominent examples being the java.lang.String class

immutable-defined

How is this related to Java?

An ‘immutable’ object is one whose instance cannot be changed once created.

How to ensure ‘immutability’?

Well, general OO rules apply here as well (think encapsulation)

  • All fields (member variables) should be made private and final
  • Do not provide any ‘setter‘ methods (mutators)
  • Don’t allow your class to be extended – you can either declare the class final or make the constructor private. In case you choose to use a private constructor, use a static factory method to create instances for the clients (in a defensive manner of course!)
  • Make defensive copies of any ‘mutable’ members which you might have as a part of your class i.e. do not provide direct access to the references of the mutable members. Otherwise, the client API may unknowingly change the state of the mutable object which you returned which in turn will create loads of other issues

Click to see the code snippet below

Lets look at the client class which creates ‘mutable’ instance of the Person class

When we execute this class, the results are as follows (not so good)

Abhishek was born on Mon Feb 17 19:19:00 EST 2014
Confirming Abhishek’s DOB:::: Wed Dec 31 19:00:00 EST 1969

Let’s look at an improved version – Immutable version of the Person class

You should consider immutability of your classes if you need

  • Thread safety: Immutable objects do not need synchronization and are unharmed even during concurrent access by multiple threads.
  • Simplicity: Dealing with classes which would encapsulate your immutable object (via composition)

To sum it up

It’s not as if you have to make every class immutable! But do remember that one should aim for immutability until and unless there is a very strong reason for not doing so e.g. you are writing an API and you know that your mutable object is accessible within the confines of your control and not really a part of the public interface of the API. This way, you are sure that the ‘client’ class is within your control and will not undue advantage of the mutable nature of the object

That’s it……… !

Happy Reading folks :-)

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}