// lecture9a.java /* + OOP reminder - model things as objects - objects written in code as classes - classes have methods and variables that reflect attributes and behaviors - difference between object and class? class = blue print object = specific instance created using class as template + Why everything $static$ so far? - no objects created yet - $static$ means can use code DIRECTLY from class without making objects - generally, bad style! use $static$ only when have extremely common information */ // modification of oop0 from Spring 2000 // see also oop1.java for more explanation // Want to make classes out of things that have attributes and behavior // + Create classes using variables and methods // + Create objects by INSTANTIATION // + $new$ Classname(arguments) // -> gives the ADDRESS IN MEMORY (location) of the NEWLY CREATED OBJECT // -> location is called a REFERENCE // -> Why? objects too "large" to move, so instead, just pass address //------------------------------------------------------------------------- // CLASS STUDENT //------------------------------------------------------------------------- class Student { int id; // instance variable (attribute) String name; // instance variable (attribute) Student() { } // constructor (behavior -- how to create) // method (behavior -- do stuff) void describe() { System.out.println("ID: " + id + " Name: " + name); } // method (behavior -- "borrowing" builtin method) // $toString$ prints out the contents of the current object // according to the returned String defined by the programmer: /* public String toString() { return "ID: " + id + " Name: " + name; } */ } //------------------------------------------------------------------------- // CLASS lecture9a (contains main) //------------------------------------------------------------------------- public class lecture9a { public static void main(String[] args) { // Declare a reference variable of class $Student$. Student a; // Create a new object and assign the reference to the object to $a$ // ACTIVATION OF CONSTRUCTOR: $Student()$ // REFERENCE TO NEW OBJECT: $new Student()$ // REFERENCE VARIABLE: $a$ // The new object is a portion of memory with the address stored in $a$ a = new Student(); System.out.println("(1) Reference $a$: " + a); System.out.println("(2) Reference w/o assignment: " + (new Student()) ); // Shortcut: Student a = new Student(); // ^^^^^^^ ^^^^^^ ^^^^^^^^^^^^^ // type refvar ref to object // Assign an integer to the $id$ field of the object referred to by $a$ a.id = 123456; // Assign a string to the $name$ field of the object referred to by $a$ a.name = "Ira"; // Check the fields of the object referred to by $a$ System.out.println("Reference after assignment: " + a); System.out.println("ID Field: " + a.id); System.out.println("ID name: " + a.name); } } /* output: (1) Reference $a$: Student@1dacd3fe (2) Reference w/o assignment: Student@1dacd436 Reference after assignment: Student@1dacd3fc ID Field: 123456 ID name: Ira */ /* output using $toString$ (1) Reference $a$: ID: 0 Name: null (2) Reference w/o assignment: ID: 0 Name: null Reference after assignment: ID: 123456 Name: Ira ID Field: 123456 ID name: Ira */