The Anatomy of a Java Application |
The bold line in theimport java.util.Date; class DateApp { public static void main(String args[]) { Date today = new Date(); System.out.println(today); } }DateApp
application listing above begins a class definition block. A class--the basic building block of an object-oriented language such as the Java language--is a template that describes the data and behaviour associated with instances of that class. When you instantiate a class you create an object that will look and feel like other instances of the same class. The data associated with a class or object are called variables; the behaviour associated with class or object are called methods.Julia Child's recipe for rack of lamb is a real-world example of a class. Her rendition of the rack of lamb is one instance of the recipe, and mine is quite another. (While both racks of lamb may "look and feel" the same, I imagine that they "smell and taste" differently.)
A more traditional example of a class from the world of programming is a class that represents a rectangle. The class would contain variables for the origin of the rectangle, and its width and height. The class would also contain a method that calculates the area of the rectangle. An instance of the rectangle class would contain the information for a specific rectangle: like the dimensions of the floor of your office, or the dimensions of the window you are using to view this page.
In the Java language, the general form of a class definition is
where the keywordclass name { . . . }class
begins the class definition for a class namedname
. The variables and methods of the class are embraced by the curly brackets that begin and end the class definition block.DateApp
has no variables and has a single method namedmain()
.
The Anatomy of a Java Application |