Package Access

If no access modifier (public, protected or privateprotected is discussed in Chapter 9) is specified for a method or variable when it is 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.

The application in Fig. 8.20 demonstrates package access. The application contains two classes in one source-code filethe PackageDataTest application class (lines 521) and the PackageData class (lines 2441). When you compile this program, the compiler produces two separate .class filesPackageDataTest.class and PackageData.class. The compiler places the two .class files in the same directory, so the classes are considered to be part of the same package. Since they are part of the same package, class PackageDataTest is allowed to modify the package-access data of PackageData objects.

Figure 8.20. Package-access members of a class are accessible by other classes in the same package.

(This item is displayed on page 397 in the print version)

1 // Fig. 8.20: PackageDataTest.java 2 // Package-access members of a class are accessible by other classes 3 // in the same package. 4 5 public class PackageDataTest 6 { 7 public static void main( String args[] ) 8 { 9 PackageData packageData = new PackageData(); 10 11 // output String representation of packageData 12 System.out.printf( "After instantiation: %s ", packageData ); 13 14 // change package access data in packageData object 15 packageData.number = 77; 16 packageData.string = "Goodbye"; 17 18 // output String representation of packageData 19 System.out.printf( " After changing values: %s ", packageData ); 20 } // end main 21 } // end class PackageDataTest 22 23 // class with package access instance variables 24 class PackageData 25 { 26 int number; // package-access instance variable 27 String string; // package-access instance variable 28 29 // constructor 30 public PackageData() 31 { 32 number = 0; 33 string = "Hello"; 34 } // end PackageData constructor 35 36 // return PackageData object String representation 37 public String toString() 38 { 39 return String.format( "number: %d; string: %s", number, string ); 40 } // end method toString 41 } // end class PackageData  

After instantiation: number: 0; string: Hello After changing values: number: 77; string: Goodbye  

In the PackageData class declaration, lines 2627 declare the instance variables number and string with no access modifierstherefore, these are package-access instance variables. The PackageDataTest application's main method creates an instance of the PackageData class (line 9) to demonstrate the ability to modify the PackageData instance variables directly (as shown on lines 1516). The results of the modification can be seen in the output window.

Категории