Java Reference
In-Depth Information
Choosing Access Attributes
As you can see from the table of access attributes, all the classes we have defined so far have had
members that are freely accessible within the same package. This applies both to the methods and the
variables that were defined in the classes. This is not good object-oriented programming practice. As we
said in Chapter 1, one of the ideas behind objects is to keep the data members encapsulated so they
cannot be modified by all and sundry, even from other classes within the same package. On the other
hand, the methods in your classes generally need to be accessible. They provide the outside interface to
the class and define the set of operations that are possible with objects of the class. Therefore in the
majority of situations with simple classes (i.e. no sub-classes), you should be explicitly specifying your
class members as either public or private , rather than omitting the access attributes.
Broadly, unless you have good reasons for declaring them otherwise, the variables in a public class
should be private and the methods that will be called from outside the class should be public . Even
where access to the values of the variables from outside a class is necessary, you don't need to make
them public or leave them without an access attribute. As we've just seen, you can provide access
quite easily by adding a simple public method to return the value of a data member.
Of course, there are always exceptions:
For classes in a package that are not public, and therefore not accessible outside the package,
it may sometimes be convenient to allow other classes in the package direct access to the
data members.
If you have data members that have been specified as final so that their values are fixed, and
they are likely to be useful outside the class, you might as well declare them to be public .
You may well have methods in a class that are only intended to be used internally by other
methods in the class. In this case you should specify these as private .
In a class like the standard class, Math , which is just a convenient container for utility
functions and standard data values, you will want to make everything public .
All of this applies to simple classes. We will see in the next chapter, when we will be looking at sub-
classes, that there are some further aspects of class structure that you must take into account.
Using a Package and Access Attributes
Let's put together an example that uses a package that we will create. We could put the Point and
Line classes that we defined earlier in a package we could call Geometry . We can then write a
program that will import these classes and test them.
Try It Out - Packaging Up the Line and Point Classes
The source and .class files for each class in the package must be in a directory with the name
Geometry . Remember that you need to ensure the path to the directory (or directories if you are
storing .class files separately) Geometry appears in the CLASSPATH environment variable setting
before you try compile or use either of these two classes. You can do this by specifying the -
classpath option when you run the compiler or the interpreter.
Search WWH ::




Custom Search