Class Immutability

“Classes should be immutable unless there’s a very good reason to
make them mutable….If a class cannot be made immutable, limit its mutability as much as possible.”

– Josh Bloch

I never thought of class immutability and its importance until I read Josh Bloch’s statement quoted on a site about Java practices. There must be something about it that Bloch would quote such a tough statement. And it’s true! A programmer must defend his class’s instance variables from being changed from outside itself (except for instantiating the class, in which case you’ll use a constructor or a static factory), unless there is a very good reason for it. Yeah, that is coolness to the uttermost level. Another point learned. :]

Continually learning,


About Jeffrey A. Aborot

> Background: BS Computer Science, University of the Philippines Baguio. > Work: Advanced Science and Technology Institute - Department of Science and Technology of the Philippines. > Academics: MS Computer Science (on-going), Algorithms and Complexity Laboratory, Computer Science Department, UP Diliman > Languages: Filipino, Tagalog, Cuyunon, English, Java, Python, C. > Operating Systems: Linux, OSX. > Weird Stuff: Bunch of Pentax film cams.
This entry was posted in Software Development. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s