Java Reference
In-Depth Information
secondStudent.id = 2;
secondStudent.firstName = "Sophie";
System.out.println("The student object referred to "+
"by the variable secondStudent has the first "+
"name: "+secondStudent.firstName);
}
}
8.
The behavior of managing students is now cleanly separated from the behavior a student itself
exposes. Later, you might want to create an Administrator class to perform this sort of manage-
ment, but for now, all you want to do is run this simple example program, so the current way of
doing things is fine to illustrate the idea.
Figure 4-2 represents the creation of objects from classes, schematically.
class Student
Student myFirstStudent
intid
boolean isBirthDay()
intid = 1
boolean isBirthDay()
String
void giveWarningBoolean
String
void giveWarning(Boolean
new Student()
String lastName
int numberOfFriends()
String lastName
int numberOfFriends()
int birthYear
int birthYear =
int birthMonth
int birthMonth
int birthDay
int birthDay =
Student mySecondStudent
new Student()
intid = 2
boolean isBirthDay()
void giveWarning(Boolean
String
String lastName
int numberOfFriends()
int birthYear =
int birthMonth
int birthDay =
figureĀ 4-2
You have now seen how to create simple class definitions in Java, containing variables and methods.
By now, you should grasp the differences between classes and objects, and should be able to define
classes and to instantiate them. You've been taken on a quick tour through the concepts of classes
and objects in Java, so now that you get the bigger picture, you can take a step back and learn more
about variables and methods, with all their intricacies, which is exactly what you will do in the fol-
lowing sections.
Search WWH ::




Custom Search