public class Rectangle { // the Rectangle class has 3 fields private int width; private int height; private Point origin; private int id = initializeId(); // add a class variable for the // number of Rectangle objects instantiated private static int numberOfRectangles = initNumberOfRectangles(); public Rectangle(int initWidth, int initHeight, Point initOrigin) { this(initHeight, initHeight); origin = initOrigin; } public Rectangle(int initWidth, int initHeight) { width = initWidth; height = initHeight; origin = new Point(0,0); id = ++numberOfRectangles; System.out.println("Initialized object id in constructor, id:" + id); } // static method for initializing static variable. private static int initNumberOfRectangles() { System.out.println("initialize numberOfRectangles in static method"); return 0; } //static initialization block static { System.out.println("initialize numberOfRectangles in static block"); numberOfRectangles = 0; } //non-static initialization block { id = 100; System.out.println("initialize objectId in block, id: " + id); } //final method private final int initializeId() { int _id = 200; System.out.println("initialize objectId in final method, id: " + _id ); return _id; } public int getID() { return id; } public static int getNumberOfRectangles() { return numberOfRectangles; } public void setWidth(int newWidth ) { width = newWidth; } public int getWidth() { return width; } public void setHeight(int newHeight ) { height = newHeight; } public int getHeight() { return height; } public void setOrigin(Point newOrigin) { origin = newOrigin; } public Point getOrigin() { return origin; } public int getArea() { return width * height; } // Move rectangle origin by x,y public void move(int dx, int dy) { origin.setX( origin.getX() + dx ); origin.setY( origin.getY() + dy ); } public String toString(String objName) { return "["+objName+"." + id + "] Width: " + width + ", Height: " + height + ", Origin: " + origin; } public static void main(String []args) { System.out.println("-------------------------------"); Point p = new Point(10,20); Rectangle rectOne = new Rectangle(30,40, p); System.out.println(rectOne.toString("rectOne")); System.out.println("-------------------------------"); System.out.println("Number of Rectangles: " + getNumberOfRectangles() ); } }