Cracking Core Java Interviews 3rd Edition

A Comprehensive Guide to Crack Core Java Interviews in Investment Banks, HealthCare IT & Startups. It covers Core Java, Algorithms, Data Structures, Concurrency, Hibernate and Spring MVC.

Specifically for investment banking domain, healthcare IT and product companies i.e. UBS, RBS, Blackrock, Morgan Stanley, JP Morgan, Nomura, Barclays, Citibank, Markit, Bank of America, Goldman Sachs and other companies i.e. Global Logic, Adobe, hCentive, Edifecs, Expedia, Infosys, TCS, Sapient, Wipro, HCL etc.
Free Chapters PDF
Buy Full PDF ₹250
3rd Edition
Last Updated : Thursday, December 3, 2015 6:51:13 PM IST Total Page Hits 511

What is Immutable Class. Where would you use it ? How would you make a class immutable ?

When the state of object can not be changed after its construction then the object is called Immutable.

Where do we need them

Immutable objects are inherently thread-safe, thus help writing multi-threading code without much worries. Immutable questions are meant for multi-threading program. If someone is talking bout immutability then indirectly he is talking about multi-threaded context. Immutable classes are easy to understand, as they possess a single state, which is controlled by their constructor. Immutable objects are good candidate for hash keys because their hashcode can be cached and reused for better performance.

Which Objects should be made Immutable

Immutable classes are ideal for representing ADT’s (Abstract Data Type) value.

Joshua Bloch suggests that
“All classes should be designed to be immutable unless there is a specific reason not to do so”

Guidelines for Making a class Immutable

  1. All fields should be declared final

  2. Class itself is declared final so that the derived classes do not make it Mutable.

  3. this reference should not be allowed to escape during object construction such as in anonymous inner classes (for example adding action listener)

  4. Any field that contains reference to mutable objects (such as arrays, collections, StringBuffer, etc)

    i. Are private

    ii. Are never returned or exposed to the caller

    iii. Are the only reference to the Objects that they refer

    iv. Do not change the state of the referenced object after the construction.

    v. If mutable fields must be returned to the caller, then a defensive copy should be returned so that the changes do not reflect in the inner data structure.

For Example,

public List getList() {
     return Collections.unmodifiableList(list); <=== defensive copy of the mutable field before returning it to caller

vi. If a mutable Object is passed in the constructor (like an array), then Immutable class should first make a defensive copy of the mutable object before storing its reference.

Similar Articles

1. Synechron Java Interview Questions

Collection of Java Interview Questions (Core Java, Spring, database and other concepts) for Synechron in banking and finance domain

2. Design Metro Smart Card System for Delhi using Java

Design a program in Java for Metro Smart Card System in Delhi. Evaluation criteria will be based on code completeness, code structure and quality, modularity, usage of OO principles, choice of data structure and unit tests.

3. What does volatile keyword do in a multi-threading environment

volatile keyword helps programmers write thread safe program