Wednesday, October 16, 2013

Immutable class

Immutable class is the one in java whose contents can not be changed once initialized. e.g all the Wrapper classes in java e.g String, Integer, Boolean, Character, Byte, Short, Long, Float, Double, BigDecimal, BigInteger are immutable.

Creating an immutable class:-
  • Declare the class as final.


Features:-
  • They are thread safe automatically so no need to synchronize.
  • Once created the state of the object can not be changed so there is no possibility of going to inconsistent state.
  • The reference can be easily shared and cached without the need to copy them
  • They can be used as keys for the map

No comments:

Post a Comment

SpringBoot Application Event Listeners

When a spring boot application starts few events occurs in below order ApplicationStartingEvent ApplicationEnvironmentPreparedEvent Applicat...