Esempio: Creazione di Oggetti della classe Student


Questa sequenza di comandi crea la collezione di oggetti vista nella figura di prima.

Student std = new Student();  // Declare a variable, std,
                              //   and initialize it with a
                              //   reference to a newly created
                              //   object of the class Student.
Student std1 = new Student(); // Declare std1, and initialize
                              //   it to refer to another
                              //   new object.
Student std2 = std1;          // Declare std2, and initialize
                              //   it to refer to the SAME
                              //   object that std1 refers to.
Student std3;                 // Declare std3, and initialize
                              //   it to null.  (This is done
                              //   automatically.)
std.name = "John Smith";
std.ID = Student.getUniqueID();
std1.name = "Mary Jones";
std1.ID = Student.getUniqueID();

                // (Other instance variables have default
                //  initial values of zero.)


Come per il caso degli array, le operazioni su variabili di tipo oggetto sono operazioni su riferimenti, non sugli oggetti stessi. Quindi


arrow1_left.gif (1097 byte)arrow1_right.gif (1095 byte)