std1 = new Student("Mary Jones");che sarebbe chiaramente da evitare... Per impedire questo, possiamo dichiarale private, come nel seguente codice:
std1.name = ("Paolo Rossi");
std1.ID = std.ID
private
String name; // Student's name
private
int ID; // unique ID number
public
double test1, test2, test3;
// grades on three tests
private
static int nextUniqueID = 1;
// next available unique ID number
Student(String
theName) {
// constructor for Student objects;
// provides a name for the Student,
// and assigns the student a unique
// ID number
name = theName;
ID = nextUniqueID;
nextUniqueID++;
}
public
String getName() {
// accessor method for reading value of private
// instance variable, name
return name;
}
public
int getID() {
// accessor method for reading value of ID
return ID;
}
public
double getAverage() { // compute average test grade
return (test1 + test2 + test3) / 3;
}
} // end of class
Student