package oopintro;

public class ClassVariables {

    /*
     * Class variables are different from instance variables:
     * An instance variable applies to one instance e.g. room.length applies
     * to one specific room object.
     * A class variable applies to all instances.  If you have one instance
     * or many instances, they all share the same class variable values.
     * It might be difficult to understand, so here's an example:
     */

     public static void main(String[] args) {

        Thing t1 = new Thing();
        t1.thingID = 100;        

        // you SHOULDN'T access a class variable via an object, 
        // you SHOULD access a class variable via the class name
        // t1.numberOfThings = 1;  // bad code
        Thing.numberOfThings = 1;

        // you SHOULDN'T invoke a class method on an object, 
        // you SHOULD invoke a class method on the name of the 
        // class
        //t1.classMethod(); // bad code
        Thing.classMethod();

        // you can only access instance methods and instance 
        // variables via an instance
        t1.instanceMethod();     
        System.out.println("Thing#: " + t1.thingID);

        // adding a new Thing and giving it a number, incrementing 
        // # of things we have
        Thing t2 = new Thing();
        t2.thingID = 101;
        Thing.numberOfThings++;
        t2.instanceMethod();  // invoking instance method on instance

       // note that numberOfThings is the same value no matter which 
       // object uses it: the value is shared among all instances
       t1.instanceMethod();
       t2.instanceMethod();  
        
     }
}
