Java Reference
In-Depth Information
Private Variables
All object-oriented languages provide some way to ensure that variables can be
shielded from inappropriate outside interference, such as when a class is called
from a driver class. In Java, the modifier, private , is used to ensure that the
driver class cannot change a variable inadvertently. A driver class, thus, cannot
access a private variable directly; instead, the driver class must access the private
variable through methods designed specifically for that purpose. By contrast, any
class in any package can access a public variable.
Declaring private variables creates values that are local in scope and should
be used only in the processes and calculations of the class in which they are
declared; the outside user has no need to see them or potentially to corrupt
them. A key principle of encapsulation is that a class should reveal to the user
only what has to be revealed and no more; this concept is referred to as the prin-
ciple of least privilege and will be discussed in more detail in Chapter 9. In this
way, data is protected from misuse, and the class can operate on a more secure
basis.
Entering Variables into the Calculator Application
The Calculator application will declare private identifiers for its components
and variables. Therefore, a driver class that extends the Calculator class will not
be able to change or corrupt any of the components, so it cannot add new but-
tons, edit the captions, or change the mathematical operations. This does not
mean, however, that driver classes cannot use the functions of the buttons and
menu. A driver class that calls the Calculator can access the public constructor
method to return the value of any of the private values.
The access modifier, private, forces those components and instance variables
to have class scope. Class scope means that these components and instance vari-
ables are not accessible outside the class. Other classes, or clients, that create
instances of the Calculator then can use the class without knowing the internal
details of how it is implemented or the variables it uses.
The Calculator application will have no get() or set() methods to change any
of the values for the variables that will hold the numbers, operators, results, and
flags. Recall that a flag is a value that triggers a change in a selection or repetition
structure. In Chapter 4, you used a flag variable named done to notify a while
loop whether or not to execute. The Calculator application uses private variables
named first, foundKey, and clearText as boolean flags to detect a button click and
to clear the TextField display.
Table 6-2 briefly explains the purpose of each of the variables used in the
Calculator application. Each variable will be explained further as it is used later
in the chapter.
Declaring Private Variables
When you declare private variables, they are local in scope and
should be used only in the processes and calculations of the class
in which they are declared; this eliminates the potential for them
to become corrupted by outside users.
Search WWH ::




Custom Search