站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

Defining a Class - Java Tutorial 5.0 英文版

The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Getting Started
Lesson: A Closer Look at the "Hello World" Application

Defining a Class

The first bold line in the following listing begins a class definition block.
/** 
 * The HelloWorldApp class implements an application that
 * simply displays "Hello World!" to the standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); //Display the string.
    }
}

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. Methods are similar to the functions or procedures in procedural languages such as C.

As an example, consider 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 and returns 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 programming language, the simplest form of a class definition is

class name {
    . . .
}

The keyword class begins the class definition for a class named name. The variables and methods of the class are enclosed within the curly brackets that begin and end the class definition block. The "Hello World" application has no variables and has a single method named main.

For more information about object-oriented concepts, see Object-Oriented Programming Concepts (in the Getting Started trail). To learn how object-oriented concepts are implemented in the Java language, see Classes and Inheritance (in the Getting Started trail).


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.