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 section Controlling Access to Members of a Class (in the Learning the Java Language trail). 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 Classes (in the Learning the Java Language trail) lists the possible elements of a member variable declaration and the table in the section Declaring Member Variables (in the Learning the Java Language trail) lists the possible elements of a method declaration. Constructors initialize a new instance of a class and have the same name as the class.

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 section Defining Methods (in the Learning the Java Language trail) 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 instance 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.