/*

 * Purposes:

 *  A. This application tests Java's access modifiers for instance variables.

 *     Both private and package-private are tested.

 *  B. It also tests the cases where an object references another object of the same class.

 *

 * To test the application: Place both classes (Main and Main2) in the same folder/package.

 */

        // When an object of a class has a reference to another object of the same class, the first object can access all

        // the second object's data and methods, including those that are private.

        //

        //Cases and Rationale:

        //

        //case 1: The second object is one of the instance variables of the first object.

        // Q: Would doing so create infinite references to objects of the same type?

        // A: No, because the referenced object is initially null.

        //

        //case 2: The second object is a local variable in one of the methods of the first object.

 

A.    Source files:

Main.java                     Main2.java

B.    Running the application using NetBeans:

1.   Download both files into a temporary folder.

2.   Create a new project with existing sources; name the project referencingObjectsOfSameClassDemo and add both files into the project.

3.   The figure below shows a sample output of running this application.