Home
Username:
Password:
Java 6 Tutorials

Concurrency / An Immutable Class




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

An immutable object is one that cannot be modified. The only way to get a modified version of an immutable object is to instantiate a new object with the differences. The overhead involved with creating a new object is not as great as you might think. Java is very efficient at constructing objects from classes and there is a savings in the amount of code necessary to protect immutable objects and the garbage collection is actually simplified. You are familiar with the string class and the advantages you have from it being an immutable class. You know that you can store a reference to a string somewhere and it will be the same when you come back. There are a few specific things you need to watch out for when you write an immutable class. This is an example of an immutable class, its not the best coding style possible but its shaped this way to show you the things you need to do for immutability. First you make all fields final and private, they need to be private so they can't be modified externally, making them final is really for your own protection, so you won't accidentally write code, that writes to them. Being final they can only be written to when object is constructed, such as in the constructor. Any other time you try to write to it you'll get a compiler error this means that you can't invariantly write sent methods to change the value of the variable. Also, you shouldn't allow sub classes of this class, the easiest way to do that is to declare the class as final as I've done here. Another way to do it is to declare the constructor as private and write a static factory method, you produce objects out of the class. If you need to store internally the reference to a immutable object don't let anything outside the class have access to it. In this example it's passed an argument on the constructor and a copy of it is made. That's because the original rectangle object could be modified externally later and that would break immutability. Synchronizing the assignment code in the constructor is a matter of thread safety. This object could possibly be accessed by a second thread and the access could occur in the middle of construction causing an erroneous value to be returned from the get method. You can write get method for all the values but be careful, if your class contains a mutable object then return only a copy of it in the get method. Of course if your class contains an immutable object such as a string you can return that. In the next lesson we'll start looking into how Java can talk over the Internet.

Tutorial Information

Course: Java 6
Author: Arthur Griffith
SKU: 33858
ISBN: 1-934743-59-3
Release Date: 2008-02-29
Duration: 7 hrs / 92 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available