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.
- Depicts an object and its reference variable, named
Figure J6-2:
- Illustrates an object in the list named
nameListbefore and after a change occurs to the object.
- Illustrates an object in the list named
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 namegetLast(): returns the last namegetName(): returns the full namesetFirst(firstName): sets the first namesetLast(lastName): sets the last namesetName(firstName, lastName): sets both namesgiveLastNameTo(aName): presumably transfers the last name to another Name objecttoString(): provides a string representation of the Name objectgetImmutable(): methods indicating potential immutability transformations
- Private fields:
ImmutableName:- Private fields:
first,last - Public Methods:
getFirst(): returns the first namegetLast(): returns the last namegetName(): returns the full nametoString(): provides a string representation of the ImmutableName objectgetMutable(): indicates possible mutable alterations to the immutable object
- Private fields:
Conclusion
- End of Java Interlude 6 note provided in the transcript.