Java Reference
In-Depth Information
ting is assumed. In this chapter, we will be concerned with
public
and
private
. The
pro-
When a member of a class is modified by the
public
specifier, that member can be ac-
cessed by any other code in your program. This includes methods defined inside other
classes.
When a member of a class is specified as
private
, that member can be accessed only by
other members of its class. Thus, methods in other classes cannot access a
private
member
of another class.
The default access setting (in which no access modifier is used) is the same as
public
unless your program is broken down into packages. A
package
is, essentially, a grouping
of classes. Packages are both an organizational and an access control feature, but a discus-
sion of packages must wait until
Chapter 8
. For the types of programs shown in this and
the preceding chapters,
public
access is the same as default access.
An access modifier precedes the rest of a member's type specification. That is, it must
begin a member's declaration statement. Here are some examples:
To understand the effects of
public
and
private
, consider the following program: