Java Reference
In-Depth Information
Now that I am talking about constructors and Student objects being created without a name, it's
a good time to emphasize another important aspect, namely the fact that the name you give your
Student variable has nothing to do with the variables of the Student . An example will help to illus-
trate this. Say you define a student as follows:
Student marc = new Student();
Even though the variable is named marc , this does not mean that the firstName variable of this
object must be equal to marc . Of course, it makes sense to use marc as a variable name for the
student named Marc, and no one in their right mind would use sophie as a variable name for the
student named Marc, but there is nothing prohibiting you from doing so. The name of the Student
variable is just a handle to refer to the Student object itself, and has nothing to do with the inter-
nals of that object.
The following Try It Out will show you how to create some students in the example course adminis-
tration program.
Creating Student Objects 
try it out
You will use the Student class definition as a blueprint to create a number of Student objects. If you
have not followed along with the previous Try It Outs, you should create a Student class now with the
following content:
class Student {
int id;
String firstName;
String lastName;
int birthYear, birthMonth, birthDay;
boolean isBirthday() {
// Return true if it's the student's birthday today.
return false;
}
void giveWarning(boolean isFinalWarning) {
// You should study harder!
}
int numberOfFriends() {
// Return the number of friends the student has.
return 0;
}
}
1.
Add the following main method to the Student class definition as follows. Note the use of the new
keyword to create objects:
public static void main(String[] args) {
Student firstStudent = new Student();
Student secondStudent = new Student();
firstStudent.id = 1;
firstStudent.firstName = "Marc";
Search WWH ::




Custom Search