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

Summary of Creating Classes - 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: Learning the Java Language
Lesson: Classes and Inheritance

Summary of Creating Classes

A class definition has two parts: a class declaration and a class body. For details about the elements of a class declaration, refer to the table in the Declaring Classes (in the Learning the Java Language trail) section. The class body contains member variables, methods, and constructors for the class. A class uses member variables to contain state and uses methods to implement behavior. The table in Declaring Member Variables (in the Learning the Java Language trail) lists the possible elements of a member variable declaration and the table in the Defining Methods (in the Learning the Java Language trail) section lists the possible elements of a method declaration. Constructors initialize a new instance of a class and look like methods with the same name as the class but without a return type.

You control access to member variables and methods in the same way: by using an access specifier, such as private or public, in the member's declaration. The table in Controlling Access to Members of a Class (in the Learning the Java Language trail) section lists the access specifiers and the effect of each specifier.

You specify a class member variable or a class method by using the static keyword in the member's declaration. A member that is not declared as static is implicitly an instance member. Class variables are shared by all instances of a class and can be accessed through the class name. Instances of a class get their own copy of each instance variable, which must be accessed through an instance reference.


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.