/** * This class is used to illustrate the usage of static variables and methods * as well as the "this" keyword. * * @author alchambers * @version sp17 */ public class SimpleClassController{ public static void main(String[] args){ final int NUM = 5; // Creat an array of objects of type SimpleClass System.out.println("Creating an array of " + NUM + " objects of type SimpleClass"); SimpleClass[] objects = new SimpleClass[NUM]; for(int i = 0; i < objects.length; i++){ objects[i] = new SimpleClass(); } System.out.println(); // Each object has its own copy of the instance variables System.out.println("Each object has its own copy of the instance variable"); for(int i = 0; i < objects.length; i++){ System.out.println("Object " + i + ": " + objects[i].getInstanceVariable()); } System.out.println(); // Changing the instance variable of one object does not affect the value of // the instance variables of the other objects System.out.println("Changing the value of one of the object's instance variable:"); objects[0].setInstanceVariable(-14); for(int i = 0; i < objects.length; i++){ System.out.println("Object " + i + ": " + objects[i].getInstanceVariable()); } System.out.println(); // However, a static variable belongs to a class not to any particular object of that class type // Thus, there is only one copy of such a static variable that all objects can reference. System.out.println("However, the static variable is shared amongst all objects"); for(int i = 0; i < objects.length; i++){ System.out.println("Object " + i + ": " + objects[i].getStaticVariable()); } System.out.println(); // Changing the value of the static variable is reflected in all objects of class type System.out.println("Changing the value of the static variable: "); objects[0].setStaticVariable(-44444); for(int i = 0; i < objects.length; i++){ System.out.println("Object " + i + ": " + objects[i].getStaticVariable()); } } }