WebReference.com - Part 4 of chapter 5 from Beginning Java 2 SDK 1.4 Edition, Wrox Press Ltd (5/8)
[previous] [next] |
Beginning Java 2 SDK 1.4 Edition
Controlling Access to Class Members
We have not yet discussed in any detail how accessible class members are outside a class. You know that from inside a static class method you can refer to any of the static members of the class, and a non-static method can refer to any member of the class. The degree to which variables and methods within one class are accessible from other classes is more complicated. It depends on what access attributes you have specified for the members of a class, and whether the classes are in the same package. This is why we had to understand packages first.
Using Access Attributes
Let's start by considering classes in the same package. Within a given package, any class has direct access to any other class name--for declaring variables or specifying method parameter types, for example--but the variables and methods that are members of that other class are not necessarily accessible. The accessibility of these is controlled by access attributes. You have four possibilities when specifying an access attribute for a class member, including what we have used in our examples so far--that is, not to specify anything at all--and each possibility has a different effect overall. The options you have for specifying the accessibility of a variable or a method in a class are:
Attribute | Permitted access |
No access attribute | From methods in any class in the same package. |
public | From methods in any class anywhere. |
private | Only accessible from methods inside the class. No access from outside the class at all. |
protected | From methods in any class in the same package and from any sub-class anywhere. |
The table shows you how the access attributes you set for a class member determine the parts of the
Java environment from which you can access it. We will discuss sub-classes in the next chapter, so
don't worry about these for the moment. We will be coming back to how and when you use the protected
attribute then. Note that public
, private
, and protected
are all
keywords. Specifying a member as public
makes it completely accessible, and at the other
extreme, making it private restricts
access to members of the same class.
This may sound more complicated than it actually is. Look at the next diagram, which shows the access allowed between classes within the same package.
Within a package such as package1
, only the private members of the class Class1
can't be directly accessed by a method in another class in the same package. Declaring a class member to
be private
limits its availability solely to methods in the same class.
We saw earlier that a class definition must have an access attribute of public
if it is to
be accessible from outside the package. The next diagram shows the situation where the classes, seeking
access to the members of a public class, are in different packages.
Here access is more restricted. The only members of Class1
that can be accessed from an
ordinary class, Class2
, in another package are those specified as public
. Keep
in mind that the class, Class1
, must also have been defined with the attribute
public
. From a sub-class of Class1
that is in another package, the members of
Class1
, without an access attribute, cannot be reached, and neither can the private
members--these can never be accessed externally under any circumstances.
[previous] [next] |
Created: July 29, 2002
Revised: July 29, 2002
URL: https://webreference.com/programming/java/beginning/chap5/4/5.html