// oop1.java DIS 2/14/2000 // expanded version of oop0.java // We encourage you to thoroughly experiment with this example. /* Q: Where do I put a class that's not the class that contains main? * A: You can put it in the same file. * + Using CodeWarrior: you may use the modifier public * + Using JDK: you may not have more than one "public class" per file * (You will notice that some online examples were created using JDK, * so don't be alarmed.) * * Q: Does it matter if other classes go above or below eachother? * A: No, but pick a style that is consistent. * * Q: May I put classes inside other files? * A: Yes. For CodeWarrior, store them in the same project directory. * */ //------------------------------------------------------------------------- // CLASS STUDENT //------------------------------------------------------------------------- /* public */ class Student { int id; String name; } //------------------------------------------------------------------------- // CLASS OOP1 (contains main) //------------------------------------------------------------------------- public class oop1 { public static void main(String[] args) { //------------------------------------------------------------------------- // Reference Variables //------------------------------------------------------------------------- // Declare a reference variable of class $Student$. // Try thinking of $Student$ as a special kind of "type" you're adding to // to Java. You're telling Java that a special kind of data structure // exists and you will use a variable of that "type." Student a; // The reference variable reserves a small amount of computer memory // to store some characters. These characters correspond to a particular // address in computer memory. //------------------------------------------------------------------------- // Instantiating an Object // "instantiating an object" creates an object from a given class //------------------------------------------------------------------------- // Declaring a reference variable does NOT allocate/create the object. // To instantiate an object that you can access with the reference // variable, you must create the object using the $new$ operator: // ex) a = new Student(); // + The $new$ operator tells Java to allocate memory for an object // + The $Student()$ portion (something called a constructor) tells // Java WHICH class to use and what operations to perform when // constructing the object. // + Entire portion $new Student()$ creates a reference to the object // Java just created. The code $new Student()$ produces a reference // of "type" Student. // + The assignment $a=new Student()$ stores the reference to the newly // created object in reference variable a. Note how "type gets type" // in this assignment. a = new Student(); // REMEMBER: $a$ REFERS to an object, but isn't an object! // You may also say that $a$ is the address of the object. // You may combine declaration and assignment in one line as you would // with "normal" assignment: // ex) Student a = new Student(); //------------------------------------------------------------------------- // Address //------------------------------------------------------------------------- // What does a reference variable look like? // Let's find out: System.out.println("(1) Reference $a$: " + a); // What happens if you just do the statement $new Student()$? System.out.println("(2) Reference w/o assignment: " + (new Student()) ); // In (2), an object is allocated, but no reference variable is assigned // to that object. So, without a reference variable, the object is // effectively useless in this context. But, there are times when you // may use code without a reference variable (discussed when we do // methods). // Note how the two addresses from (1) and (2) differ! // Whenever you allocate an object, you're instatiating a DIFFERENT object //------------------------------------------------------------------------- // How do you use the reference? //------------------------------------------------------------------------- // The reference tells Java where the object is located in memory. // Remember that the reference holds an address, characters that point // to where the objects exists in computer memory. // To access a field in the object, use the dot ($.$) operator: // Instance data: you may assign values to the variable fields of the // object. To know what you may assign, look at the class. The members of // the class inidicate what is available for you to assign in each object. // Note that since each object is "unique," setting fields for one object // doesn't necessarily change the fields for another. (However, there are // ways around this behavior with $static$, also coming later.) // 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("ID Field: " + a.id); System.out.println("ID name: " + a.name); } }