390 Object-Based Programming Chapter 8 Software Engineering Observation (My space web page)
390 Object-Based Programming Chapter 8 Software Engineering Observation 8.11 The class designer need not provide set and/or get methods for each private data member; these capabilities should be provided only when it makes sense and after careful thought by the class designer. Testing and Debugging Tip 8.1 Making the instance variables of a class private and the methods of the class public facilitates debugging because problems with data manipulations are localized to the class s methods. 8.5 Creating Packages As we have seen in almost every example in the text, classes and interfaces (discussed in Chapter 9) from preexisting libraries, such as the Java API, can be imported into a Java program. Each class and interface in the Java API belongs to a specific package that contains a group of related classes and interfaces. As applications become more complex, packages help programmers manage the complexity of application components. Packages also facilitate software reuse by enabling programs to import classes from other packages (as we have done in almost every example to this point). Another benefit of packages is that they provide a convention for unique class names. With hundreds of thousands of Java programmers around the world, there is a good chance that the names you choose for classes will conflict with the names that other programmers choose for their classes. This section introduces how to create your own packages and discusses the standard distribution mechanism for packages. The application of Fig. 8.4 and Fig. 8.5 illustrates how to create your own package and use a class from that package in a program. The steps for creating a reusable class are: 1. Define a public class. If the class is not public, it can be used only by other classes in the same package. 2. Choose a package name, and add a package statement to the source code file for the reusable class definition. [Note: There can be only one package statement in a Java source code file.] 3. Compile the class so it is placed in the appropriate package directory structure. 4. Import the reusable class into a program, and use the class. Common Programming Error 8.4 A syntax error occurs if any code appears in a Java file before the package statement (if there is one) in the file. We chose to demonstrate Step 1 by modifying the public class Time1 defined in Fig. 8.1. The new version is shown in Fig. 8.4. No modifications have been made to the implementation of the class, so we will not discuss the implementation details of the class again here. To satisfy Step 2, we added a packagestatement at the beginning of the file. Line 3 uses a packagestatement to define a package named com.deitel.jhtp4.ch08. Placing a packagestatement at the beginning of a Java source file indicates that the class defined in the file is part of the specified package. The only types of statements in Java that can appear outside the braces of a class definition are package statements and import statements. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services