• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

What is Immutable Objects in Java?

December 3, 2010 //  by Krishna Srinivasan//  Leave a Comment

An immutable object is something whose state can’t be change after there creation, e.g. String objects. Once you have created a String object, you can’t alter this.

Creating an Immutable object’s class

Creating an immutable object’s class can be tricky. Minimal requirement to create an immutable object’s class is, make class and every member variable, “final”.

also read:

  • Java Tutorials
  • Java EE Tutorials
  • Design Patterns Tutorials
  • Java File IO Tutorials
public final class SimpleImmutableClass {
 final int intVal = 5;
 final String sampleString = "Hello";
 final Integer intObj = Integer.valueOf(10);
}

SimpleImmutableClass is a class of mutable object, one can’t change its state, once object is created. But it is too simple case. In real world situation is far more complex, you will rarely find such a simple class that has only primitive or immutable members (actually String and Integer classes are classes for immutable objects, provided by JDK API stack).

Consider following example, that has mutable objects as members, though every member is final, private and has only getters (to access) and no setters, but still do you think that it is a class that can produce immutable objects.

public final class NotAnImmutableClass{
private final ArrayList sampleList = new ArrayList();
 private final Date today = new Date();

 public ArrayList getSampleList() {
  return sampleList;
 }
 public Date getToday() {
  return today;
 }
}

What do you think about above class, is it really an Immutable object’s class? Unfortunately, a big NO. At first it seems like it is an immutable object’s class, after all, it is a final class, every variable is final and private and to give more assurance, it doesn’t contain any “setter” method. Some may give me a good fight arguing about immutability of the objects of this class. But then comes the villain, the very basic of Java, the “Reference”. Consider the following listing:

class PokeIntoSoCalledImmutableClass {
public void poke(){
  NotAnImmutableClass notAnImmutableClass = new NotAnImmutableClass();
  List samples = notAnImmutableClass.getSampleList();
  Date d = notAnImmutableClass.getToday();
  samples.add("Poked String");
  d.setYear(111);
  System.out.println(notAnImmutableClass.getSampleList().size());
  System.out.println(notAnImmutableClass.getToday());
 }

}

What do you think, the method poke() would print? Do you think it would print “0” and “Sun Jun 05 00:00:00 IST 2010”? Answer is a big NO!!!

So, here comes another rule for a class whose objects to be immutable, either it should not have any mutable field (as in our case) or it should not publish (expose) its mutable members.

But again question is, what if we have to expose mutable objects and still we need that our class’s objects are immutable? Well answer is simple but tricky. USE CLONING!!!! If exposed object contains only immutable objects or primitive types, use shallow copy, otherwise use deep copy.

Correct way to implement Immutable class:

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AnImmutableClass {
 private final ArrayList sampleList = new ArrayList();
 private final Date today = new Date(110,5,5);

 public ArrayList getSampleList() {
  ArrayList returnList = new ArrayList();
  for(String s : sampleList){
   returnList.add(s);
  }
  return returnList;
 }
 public Date getToday() {
  return (Date)today.clone();
 }

 public static void main(String[] args) {
  PokeIntoSoCalledImmutableClass pokeClass = new PokeIntoSoCalledImmutableClass();
  pokeClass.poke();
 }

}

class PokeIntoSoCalledImmutableClass {
 public void poke(){
  AnImmutableClass anImmutableClass = new AnImmutableClass();
  List samples = anImmutableClass.getSampleList();
  Date d = anImmutableClass.getToday();
  samples.add("Poked String");
  d.setYear(111);
  System.out.println(anImmutableClass.getSampleList().size());
  System.out.println(anImmutableClass.getToday());
 }

}

I here conclude with set of rules to make a class whose objects are immutable:

  • Must be final
  • Only final members should be exposed to outer world
  • Exposed members should be immutable
  • If it is require to expose mutable members, then use shallow or deep cloning while exposing them

 

Category: JavaTag: Core Java

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Novice way to implement a Singleton
Next Post: Creating Database aware applications in Ruby on Rails »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact