Java Interlude 6: Mutable and Immutable Objects Study Notes

Copyright Information

  • Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
  • Mention of the textbook: Data Structures and Abstractions with Java ™ 5th Edition
  • Subject: Java Interlude 6 - Mutable and Immutable Objects

Mutable Objects

  • Definition and Properties:
    • Mutable Objects are those that can change their state or data after they have been created.
    • They typically have private data fields.
    • These data fields are accessed and modified through public methods.
      • Accessor Methods:
      • Methods that allow reading of the private fields without changing them.
      • Mutator Methods:
      • Methods that allow modification of the private fields, enabling the object to change state.
    • An object that belongs to a class with public mutator methods is referred to as mutable.

Object Representation

  • Figure J6-1:

    • Depicts an object and its reference variable, named chris.
  • Figure J6-2:

    • Illustrates an object in the list named nameList before and after a change occurs to the object.

Class Structures Involving Mutable and Immutable Objects

  • Figure J6-3:
    • Illustrates class structures for mutable and immutable names:
    • Classes Involved:
    • Name:
      • Private fields: first, last
      • Public Methods:
      • getFirst(): returns the first name
      • getLast(): returns the last name
      • getName(): returns the full name
      • setFirst(firstName): sets the first name
      • setLast(lastName): sets the last name
      • setName(firstName, lastName): sets both names
      • giveLastNameTo(aName): presumably transfers the last name to another Name object
      • toString(): provides a string representation of the Name object
      • getImmutable(): methods indicating potential immutability transformations
    • ImmutableName:
      • Private fields: first, last
      • Public Methods:
      • getFirst(): returns the first name
      • getLast(): returns the last name
      • getName(): returns the full name
      • toString(): provides a string representation of the ImmutableName object
      • getMutable(): indicates possible mutable alterations to the immutable object

Conclusion

  • End of Java Interlude 6 note provided in the transcript.