String Class Part 2 Tutorial

The String class is immutable, what does that mean? An immutable object is an object whose state cannot be modified after it is created. Java uses the term state to describe the values that variables (fields) hold for an instance of an object. In this tutorial, I will create a mock String class that is similar to the real deal and demonstrate how to make it immutable.

Immutable Characteristics

  • Apply the final modifier to the class to ensure it cannot be inherited (become a superclass).
  • Instance variables (fields) should be private and final.
  • Do not create a no-argument constructor.
  • Create a constructor(s) that initializes the private final instance variables. This ensures that they can only be initialized once.
  • Remember that Java is a pass-by-value, so any reference variables received as constructor parameters must use a technique called defensive copying.
  • Do not create any setter methods that change the state (values) of the object in any way.

This tutorial will build on concepts learned from my Final Classes Tutorial, Passing Variables to Methods and Constructors Tutorial, and Final Reference Variables Tutorial.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md StringTwo
C:\Java>cd StringTwo
C:\Java\StringTwo>Notepad StringTwo.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.



class StringTwo {
	public static void main(String args[]) {
		char myChars[] = { 'A', 'B', 'C' }; // an array is an object
		StringLike s = new StringLike(myChars);
		System.out.println(s);
	}
}

class StringLike {
	private char value[]; 

	StringLike(char value[]) {
		this.value = value;
	}

}


//class StringTwo extends StringLike {
        /* myChars[0] = 'D';
        myChars[1] = 'E';
        myChars[2] = 'F';
        //s.changeValue(myChars);
        System.out.println(s);
        */
//final class StringLike { // final class cannot be extended
//private final char value[]; // once assigned, final reference cannot be reassigned to a different object instance

    //void changeValue(char value[]) {
    //	this.value = value; // pass-by-value, remember an array is an object!
    //}	
    //@Override
    //public String toString() {
    //	return new String(this.value);	
    //}	

//import java.util.Arrays;
        //this.value = Arrays.copyOf(value, value.length); // defensive copying


Now switch back to the command prompt (CMD) and type in javac StringTwo.java and press Enter.
Now type in java StringTwo and press Enter.


C:\Java\StringTwo>javac StringTwo.java
C:\Java\StringTwo>java StringTwo
results vary


Final thoughts

An immutable object is an object whose state cannot be modified after it is created. As your knowledge of Java advances, there are other considerations to keep in mind to ensure that a class is immutable. The rules that I laid out in this tutorial will more than serve as an excellent understanding of what it takes to make a class immutable.


Tutorials