By Tony Bevis on Saturday, 14 October 2023
Category: Java

Java programming course: 9.1 Immutable classes and the Object class

In the previous lesson we built the Person utility class.

Immutable classes and the Object class

 Immutable classes are those which don't enable their state to change after they have been constructed. The Object class provides a core set of functionalities inherited by every Java class, including those you define yourself.

In this section you will learn:

Developing an immutable class

The ZooKeeper and Visitor classes each currently define an email attribute of type String to store an email address. Because emails need to be in a prescribed format, and are applicable to many applications, it would make sense to define another utility class to model this rather than a using plain String object directly. You will therefore write a class called Email in com.example.util with the following features:

This is not intended to be a full implementation of a class to model an email. In particular, the validation described above does not detect all possible incorrect email formats.

You may have noticed that no setter methods were specified: only getters. This is known as an immutable class – once the state of an object has been set through its constructor then it cannot be changed. Immutable classes offer several advantages over mutable ones, and if you see an opportunity to make a class immutable then you should seriously consider doing so. As mentioned previously, the ubiquitous String class is immutable, as are several other Java supplied classes.

The natural question to ask, therefore, is how to handle the inevitable time when a person's email address changes? The answer is straightforward: simply create a brand-new Email object and discard the old one. This applies even to simple changes such as correcting a spelling mistake and is a reasonable approach since modifications to email address are relatively uncommon.

In com.example.util define a new class called Email:

You need a method to return the local-part (i.e., the part before the @ symbol):

The indexOf() method of the String class returns the index of the first occurrence of the char or String specified in the argument. In this case it looks for @ in the email attribute. If it is not found, then -1 will be returned;


The substring() method of the String class returns a String consisting of the characters which lie between the first argument index position (which here is 0) and the second argument index position less one (which here is atIndex).


You need a similar method to return the domain-part:

You can see above that the substring() method is overloaded to accept a single argument. In this case, it returns the characters from the argument index until the end of the string


You can override the toString() method (inherited from Object) to return the full email address:

For the natural ordering you need to implement the Comparable interface:

And provide the code for the interface's compareTo() method:


You now need to write a helper method to validate the email address. If it is invalid an exception will be thrown. Define a private validate() method, initially only validating that the email contains no spaces anywhere within:


Now include another section inside the method above to ensure that there is exactly one @ character:


The next section needs to ensure the local-part is not empty:


The domain-part needs to contain at least one dot:

Finally, each String either side of any dot character must not be empty:

Some of the code in the validate() method could be simplified through the use of the String method called split(), which returns an array of String objects either side of the argument to split(). However, the argument needs to be a regular expression which is beyond the scope of this course.

With the validation method completed you need to call it from inside the constructor:

In the next lesson we will see how to prevent immutable classes from being compromised.

Next lesson: 9.2 Preventing the compromisation of immutable classes 

Related Posts

Leave Comments