Java Reference
In-Depth Information
A class is a collection of a specific number of components. The components of a class
are called the members of the class .
The general syntax for defining a class is:
modifier(s) class ClassIdentifier modifier(s)
{
classMembers
}
where modifier(s) are used to alter the behavior of the class and, usually, classMembers
consist of named constants, variable declarations, and/or methods, but can even include
other classes. That is, usually a member of a class can be a variable (to store data) or a
method or an inner class. Some of the modifiers that we have encountered are public ,
private ,and static .
￿ If a member of a class is a named constant, you declare it just like any
other named constant.
￿ If a member of a class is a variable, you declare it just like any other variable.
￿ If a member of a class is a method, you define it just like any other method.
￿ If a member of a class is a method, it can (directly) access any member of
the class—both data members and methods. Therefore, when you write
the definition of a method, you can directly access any data member of
the class (without passing it as a parameter).
￿ Later, we'll describe class members, which themselves are classes, called
inner classes.
In Java, class is a reserved word. It only defines a data type and it announces the
declaration of a class. In Java, the data members of a class are also called fields.
The members of a class are classified into four categories. The three typically used
categories are private , public , and protected . (The fourth category is described in
the next note.) This chapter discusses the private and public categories. Chapter 10
discusses protected members.
The following are some facts about private and public members of a class:
￿
If a member of a class is private , you cannot access it outside the class.
If a member of a class is public , you can access it outside the class.
￿
Recall that a package is a collection of related classes. Appendix D describes how to
create your own packages. If a class member is declared/defined without any modifiers,
then that class member can be accessed from anywhere in the package. (This is the fourth
category of class members and is called the default visibility of class members. However,
this type of visibility should be avoided.)
Search WWH ::




Custom Search