AccessorMutatorDemo.java.html

  1  import java.awt.Rectangle;
  2  
  3  /**
  4     This example demonstrates accessors and mutators.
  5  */
  6  public class AccessorMutatorDemo
  7  {
  8     public static void main(String[] args)
  9     {
 10        Rectangle box = new Rectangle(5, 10, 20, 30);
 11        System.out.print("box: ");
 12        System.out.println(box);
 13  
 14        // getWidth is an accessor method
 15  
 16        double width = box.getWidth();
 17        System.out.print("width: ");
 18        System.out.println(width);
 19  
 20        // Calling an accessor doesn't change the object
 21  
 22        System.out.print("box: ");
 23        System.out.println(box);
 24  
 25        // translate is a mutator method
 26  
 27        box.translate(15, 25);
 28  
 29        // Calling a mutator changes the object.
 30        
 31        System.out.print("box: ");
 32        System.out.println(box);
 33     }
 34  }