8.14 Package Access
If no access modifier (public
, protected
or private
—we discuss protected
in Chapter 9) is specified for a method or variable when it’s declared in a class, the method or variable is considered to have package access. In a program that consists of one class declaration, this has no specific effect. However, if a program uses multiple classes from the same package (i.e., a group of related classes), these classes can access each other’s package-access members directly through references to objects of the appropriate classes, or in the case of static
members through the class name. Package access is rarely used.
Figure 8.15 demonstrates package access. The app contains two classes in one sourcecode file—the PackageDataTest
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.