Skip to main content

Posts

Showing posts with the label Immutable class

How to create an immutable class in java

An object is immutable if its state cannot change after construction, immutable objects don’t expose any way for other objects to modify their state, the object’s fields are initialized only once inside the constructor and never change again. In this article, we define the typical steps for creating an immutable class in java, we also shed the light on the common mistakes which are normally done by developers while creating immutable classes. 1 . Usage of Immutable classes Nowadays, the  “must-have”  specification of every software application is to be distributed and multi-threaded, multi-threaded applications always cause headache to developers since developers are required to protect the state of their objects from concurrent modifications of several threads at the same time, for this purpose, developers normally use the  Synchronized  blocks whenever they modify the state of an object. With immutable classes, ...