Java Reference
In-Depth Information
Note Recall that the basic class definition, for now, looks as follows:
class CLASSNAME {
// VARIABLE DEFINITIONS
// METHOD DEFINITIONS
}
You might be wondering if this ordering is strict, meaning whether the classes
really have to start with variable definitions first, followed by method defini-
tions. The answer is no. For example, it is perfectly okay (syntactically) to write
a class like the following:
class Student {
boolean isBirthday() {
// Return true if it's the student's birthday today.
return false;
}
int id;
void giveWarning(boolean isFinalWarning) {
// You should study harder!
}
String firstName;
int numberOfFriends() {
// Return the number of friends the student has.
return 0;
}
int birthYear, birthMonth, birthDay;
String lastName;
}
While neither Eclipse nor Java will care about this, you—the programmer—
should. Stylistically, this code scores badly. It's hard to follow and will only
serve to confuse you and others later. Therefore, it's best to stick to the
convention of “variables first, methods next.” In addition, keep in mind that
code is read more often than written, no matter the language it is written in.
Therefore, it is always a good idea to keep program code as clean, organized,
structured, and readable as possible, as well as to follow common conventions
and commenting where necessary.
creating objects
You have seen how to define simple classes in Java. Remember that classes are like blueprints
or prototypes from which objects are created. For example, you defined the class Student as
a concept having a name, an ID, and some methods describing its behavior, but you have not
yet created an actual student. Now that you have defined the class, however, you can create, or
 
Search WWH ::




Custom Search