Java Reference
In-Depth Information
public void setX(double inputX) {
x = inputX;
}
It may seem odd to use a method to alter the value of a private data member when you could just make
it public . The main advantage of using a method in this way is that you can apply validity checks on the
new value that is to be set and prevent inappropriate values from being assigned. Of course, if you really
don't want to allow the value of a private member to be changed, you don't include a mutator method for
the class.
Choosing Access Attributes
As you can see from the table of access attributes, all the classes you 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 I 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
that provide the operations you want to allow with objects of the class type 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 subclasses), you should be
explicitly specifying your class members as either public or private , rather than omitting the access at-
tributes.
Broadly, unless you have good reasons for declaring them otherwise, the variables in a public class should
be private and the methods that are 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 you'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 mem-
bers.
• 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 intended to be used only internally by other methods
in the same 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 should make everything public .
All of this applies to simple classes. You see in the next chapter, when you look at subclasses, that there
are some further aspects of class structure that you must take into account.
Using Package and Access Attributes
Let's put together an example that uses a package that you create. You could put the Point and Line classes
that you defined earlier in package with the name Geometry . You can then write a program that imports
Search WWH ::




Custom Search