- Forward


Mutable and Immutable Objects Revisited
with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Recall
Back SMYC Forward
  • Mutable Objects:
    • Have attributes that can be changed after they are initialized
  • Immutable Objects:
    • Have attributes that can not be changed after they are initialized
Relevant Language Features
Back SMYC Forward
  • Related to Information Hiding:
    • Attributes can be declared public or private
  • Related to Assignment:
    • Attributes and variables can be declared final or not
Characteristics of Attributes
Back SMYC Forward
  • Classes of Mutable Objects:
    • Attributes must not be final
    • Attributes can be public or private (depending on the methods included in the class)
  • Classes of Immutable Objects:
    • Attributes must be either final or private (or both)
A Class of Mutable Objects with Public Attributes
Back SMYC Forward
public class Point { public double x, y; }
A Class of Immutable Objects with Public Attributes
Back SMYC Forward
public class Point { public final double x, y; public Point(double x, double y) { this.x = x; this.y = y; } }
A Class of Immutable Objects with Private Attributes
Back SMYC Forward
public class Point { private double x, y; public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } }
A Class of Mutable Objects with Private Attributes
Back SMYC Forward
public class Point { private double x, y; public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } }
Information Hiding and Safety
Back SMYC Forward
  • Convenience:
    • For the class of mutable objects, the implementation with public attributes seems much more convenient
  • Safety:
    • In some situations, public attributes are not safe because they can't take on all possible values
Information Hiding and Safety (cont.)
Back SMYC Forward
Another Class of Mutable Objects with Public Attributes
public class Rectangle { public double x, y; public double width, height; }

This implementation isn't safe if the width and the height can't be negative.

Information Hiding and Safety (cont.)
Back SMYC Forward
Another Class of Mutable Objects with Private Attributes
public class Rectangle { private double x, y; private double width, height; public Rectangle(double x, double y, double width, double height) { setX(x); setY(y); setWidth(width); setHeight(height); } public void setHeight(double height) { if (height < 0) { y = y + height; this.height = -height; } } public void setWidth(double width) { if (width < 0) { x = x + width; this.width = -width; } } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } }
Attributes that are Reference Types
Back SMYC Forward
  • A Reminder:
    • Attributes can be either primitive types or reference types
  • A Common Confusion:
    • The immutability of classes that have attributes that are reference types
Attributes that are Reference Types (cont.)
Back SMYC Forward
Another Rectangle Class
public class Rectangle { public final double[] corner; public final double[] size; public Rectangle(double[] corner, double[] size) { this.corner = corner; this.size = size; } }
A Use of this Rectangle Class
double[] c = { 0.0, 0.0}; double[] s = {10.0, 20.0}; Rectangle r; r = new Rectangle(c, s); s[0] = 50.0;
Attributes that are Reference Types (cont.)
Back SMYC Forward
  • But the Attributes are final?
    • Correct, but nothing is being assigned to the attributes
  • I'll Make the Attributes private!
    • That won't solve the problem
Attributes that are Reference Types (cont.)
Back SMYC Forward
Still Another Rectangle Class
public class Rectangle { private final Point corner; private final Point size; public Rectangle(Point corner, Point size) { this.corner = corner; this.size = size; } }
A Use of this Rectangle Class
Point c = new Point( 0.0, 0.0); Point s = new Point(10.0, 20.0); Rectangle r = new Rectangle(c, s); s.setX(50.0);
Attributes that are Reference Types (cont.)
Back SMYC Forward
  • What Causes the Behavior Above?
    • The use of aliases
  • Can Anything be Done?
    • Make a deep copy
Attributes that are Reference Types (cont.)
Back SMYC Forward
Still Another Rectangle Class
public class Rectangle { private final Point corner; private final Point size; public Rectangle(Point corner, Point size) { this.corner = new Point(corner.getX(), corner.getY()); this.size = new Point(size.getX(), size.getY()); } }
There's Always More to Learn
Back -