Java Reference
In-Depth Information
use the default access, which is essentially public. This means that it would be possible
for a program that uses a
Queue
to directly access the underlying array, possibly accessing
its elements out of turn. Since the entire point of a queue is to provide a first-in, first-out
list, allowing out-of-order access is not desirable. It would also be possible for a malicious
programmer to alter the values stored in the
putloc
and
getloc
indices, thus corrupting the
queue. Fortunately, these types of problems are easy to prevent by applying the
private
specifier.
1.
Copy the original
Queue
class in
Try This 5-2
to a new file called
Queue.java
.
2.
In the
Queue
class, add the
private
modifier to the
q
array, and the indices
putloc
and
getloc
, as shown here: