153k views
3 votes
Assume that the following mutable class has been declared:

public class Person
{
private String name;
public Person (String n)
{
name = n;
}
public Person (Person otherPerson)
{
name other Person. name;
}
public String getName()
{
return name;

}
public void setName(String n)
{
name = n;
}
}
Write an immutable class named Committee that has the following fields: committeeName - a String that holds the name of the committee, chairperson - a Person object that represents the chairperson of the committee.
The immutable Committee class should have the following methods:
Constructor - the constructor should accept two arguments. The first argument is a String that holds the name of the committee, and the second argument is a Person object that represents the chairperson of the committee. These arguments should be used to initialize the committeeName and chairperson fields, getCommitteeName - this method should return the value of the committeeName field, getChairperson - this method should return a Person object representing the chairperson of the committee. When writing the Committee class, be sure to follow the guidelines for immutable classes.

1 Answer

7 votes

Final answer:

An immutable Committee class in Java is designed with final fields to prevent changes after instantiation, including the use of defensive copying for mutable objects like the Person object, to ensure no changes come from external classes.

Step-by-step explanation:

Creating an immutable class in Java means once an instance of the class is created, its state cannot be modified. To write a class called Committee that adheres to these principles, you need to ensure that all of its fields are declared as final, which means they can only be assigned once, typically inside the constructor. Furthermore, any mutable objects that are included in the class (like the Person object in this case) must not change through that class.

The Committee class would look something like the following code snippet:

public final class Committee {
private final String committeeName;
private final Person chairperson;

public Committee(String committeeName, Person chairperson) {
this.committeeName = committeeName;
// Create a new Person object here to ensure immutability
this.chairperson = new Person(chairperson);
}

public String getCommitteeName() {
return committeeName;
}

public Person getChairperson() {
// Return a new Person object based on the chairperson to avoid external changes
return new Person(chairperson);
}
}

The constructor takes a String and a Person object as parameters and initializes the corresponding fields. Notice how a new Person object is created in the constructor and the getChairperson method. This approach prevents the original Person object passed to the Committee from being altered by outside classes, which upholds the immutable property of the Committee class.

By adhering to these practices, you ensure that instances of the Committee class remain immutable throughout their lifecycle, contributing to easier debugging and more reliable code when it comes to concurrent applications.

User Chad Killingsworth
by
8.5k points