// how do objects, classes, references work? // Scaled back Worker class: class Worker { // FIELDS of class consist of variables and methods // Instance variables: int id; String name; // Constructor (special kind of method that // returns address of newly // created object automatically!): Worker() { } // Methods: // $toString$ is "built-in" and prints // whatever String you wish // when you print the reference to an object String toString() { return "Worker: "+name+", ID: "+id; } } // class Worker public class Simulation { public static void main(String[] args) { // Create a new Worker object and // store the addres of that object in $worker1$. // Also called INSTANTIATING AN OBJECT/CLASS Worker worker1; worker1 = new Worker(); // you could have combined the statements into // Worker worker1 = new Worker(); // Print value of reference. Make sure you comment out $toString$ // first: System.out.println(worker1); // Set $worker1$ instance variables. // Why INSTANCE VARIABLE? // Each object is an instance of a class! // (To make variables and methods shared by // all objects instantiated from same class // use $static$ -- discussed later) worker1.id = 123456; worker1.name = "Ira"; // Print description of $worker1$. Go back and // remove comments on $toString$ method: System.out.println(worker1.toString()); // long method System.out.println(worker1); // short-cut // $toString$ is a "built-in" method that ALL classes // automatically can use -- printing a reference causes the // built-in version of $toString$ to print the reference address // Otherwise, if you provide code for $toString$, Java will // activate the code in your $toString$. } } // class Simulation