![]() ![]() ![]() ![]() |
The "Hello World" Application |
The bold line in the listing above begins a class definition block. A class--the basic building block of an object-oriented language such as Java--is a template that describes the data and behavior associated with instances of that class. When you instantiate a class you create an object that looks and feels like other instances of the same class. The data associated with a class or object is stored in variables; the behavior associated with a class or object is implemented with methods.class HelloWorldApp { public static void main (String args[]) { System.out.println("Hello World!"); } }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" different.)
A more traditional example from the world of programming is a class that represents a rectangle. The class would contain variables for the origin of the rectangle, its width, and its height. The class might 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, such as the dimensions of the floor of your office, or the dimensions of this page.
In the Java language, the simplest form of a class definition is
class name { . . . }The keyword
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. The "Hello World" application has no variables and has a single method namedmain()
.For more information about object-oriented concepts, see Object-Oriented Programming Concepts: A Primer
To learn about to implement object-oriented concepts in the Java language, see Objects, Classes, and Interfaces
![]() ![]() ![]() ![]() |
The "Hello World" Application |