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.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net