/*****
 * Arrays of objects - how things can go wrong.
 * As written, this program will crash.
 *
 ***/

class TestPointArray {

    private final static int NUMBER_OF_POINTS = 12;

    private static JimsPoint[] thePts;
    // That defined a place for "thePts", but doesn't put anything there.
    // So at this point, thePts==null.
    // thePts[3] would be an error at this point; it doesn't exist yet.

    public static void main(String[] args){
	
	thePts = new JimsPoint[NUMBER_OF_POINTS];
	// That created space for an array, but didn't put anything in it.
	// So at this point, thePts == { null, null, null, ... , null }
	// Now we can refer to thePts[3] without an error, but there
	// isn't an object there yet.

	thePts[0] = new JimsPoint(1.0,2.0);
	thePts[1] = new JimsPoint(10.0,20.0);
	thePts[2] = new JimsPoint(0.0,-10.0);
	thePts[3] = new JimsPoint(12.2, 1.2);

	thePts[4] = thePts[1];
	thePts[5] = thePts[2];

	System.out.println(" The number of points is " + JimsPoint.howMany());

	// Now there's an object in the 0'th slot.  
	// thePts == { (1.0,2.0), null, null, ..., null }

	// Try to print each point.
	for (int i=0; i<NUMBER_OF_POINTS; i++){
	    System.out.println("  slot " + i + " is " + thePts[i]);
	    if ( thePts[i] != null) {
		System.out.println(" point #"+i+" = " + thePts[i].asString());
	    }
	}

    } // end main()

} // end class


syntax highlighted by Code2HTML, v. 0.9.1