/** * Thing.java * Jeff Ondich, 1/10/14 * * A brief exploration of static and non-static data and methods. * See StaticExplorer.java. */ public class Thing { private static int thingCount = 0; private static String classMotto = "The Thing class is the best class"; private String instanceMotto = "This is the default Thing instance motto"; public Thing() { thingCount++; } public static int getThingCount() { return thingCount; } public static String getClassMotto() { return classMotto; } public static void setClassMotto(String motto) { classMotto = motto; } public String getInstanceMotto() { return instanceMotto; } public void setInstanceMotto(String motto) { instanceMotto = motto; } /* DON'T LOOK HERE YET! Who needs the StaticExplorer class, anyway. Just move its main() in here. public static void main(String[] args) { // What can we do with no Thing objects instantiated? System.out.println("=== No Things instantiated ==="); System.out.println("Class motto: " + Thing.getClassMotto()); Thing.setClassMotto("This is the fabulous and self-referential new Thing Motto!"); System.out.println("Class motto after change: " + Thing.getClassMotto()); System.out.format("We have instantiated %d Thing objects so far.\n", Thing.getThingCount()); // System.out.println("Instance motto: " + Thing.getInstanceMotto()); System.out.println(""); // Instantiate a couple Thing objects. Now what can we do? System.out.println("=== Let's instantiate two Things ==="); Thing thingOne = new Thing(); Thing thingTwo = new Thing(); System.out.format("We have instantiated %d Thing objects so far.\n", Thing.getThingCount()); System.out.println("Thing One's instance motto: " + thingOne.getInstanceMotto()); System.out.println("Thing Two's instance motto: " + thingTwo.getInstanceMotto()); System.out.println("Thing One's class motto: " + thingOne.getClassMotto()); System.out.println("Thing Two's class motto: " + thingTwo.getClassMotto()); System.out.println(""); System.out.println("=== Change thingOne's instance and class motto, then reprint all the mottos ==="); thingOne.setInstanceMotto("I'm Thing One and I'm both proud and somewhat overbearing!"); thingOne.setClassMotto("All Hail Thing One"); System.out.println("Thing One's instance motto: " + thingOne.getInstanceMotto()); System.out.println("Thing Two's instance motto: " + thingTwo.getInstanceMotto()); System.out.println("Thing One's class motto: " + thingOne.getClassMotto()); System.out.println("Thing Two's class motto: " + thingTwo.getClassMotto()); System.out.println(""); } */ }