Java 17 Recipes


-7. Organizing Code with Packages


Download 3.2 Mb.
Pdf ko'rish
bet19/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   15   16   17   18   19   20   21   22   ...   245
Bog'liq
Java 17 Recipes

1-7. Organizing Code with Packages
 Problem
Your application consists of Java classes, interfaces, and other types. You want to 
organize these source files to make them easier to maintain and avoid potential
class- naming conflicts.
ChApteR 1 GettInG StARted wIth JAvA 17


26
 Solution
Create Java packages and place source files within them. Java packages can organize 
logical groups of source files within an application. Packages can help organize code, 
reduce naming conflicts among different classes and other Java-type files, and provide 
access control. To create a package, simply create a directory within the root of your 
application source folder and name it. Packages are usually nested within each other and 
conform to a standard naming convention. For this recipe, assume that the organization 
is named JavaBook and makes widgets. To organize all the code for the widget 
application, create a group of nested packages conforming to the following directory 
structure.
/org/javabook
Any source files placed within a package must contain the package statement as the 
first line in the source. The package statement lists the name of the package in which 
the source file is contained. For instance, suppose that the main class for the widget 
application is named JavaBookWidgets.java. To place this class into a package named 
org.javabook, physically move the source file into a directory named javabook, which 
resides within the org directory, which resides within the root of the source folder for the 
application. The directory structure should look like the following.
/org/javabook/JavaBookWidgets.java
The source for JavaBookWidgets.java is as follows.
package org.javabook;
/**
* The main class for the JavaBook Widgets application.
* @author
*/
public class JavaBookWidgets {
public static void main(String[] args){
System.out println("Welcome to my app!");
}
}
ChApteR 1 GettInG StARted wIth JAvA 17


27
The first line in the source contains the package statement, which lists the name of 
the package that the source file is located within. The entire package path is listed in the 
statement, and dots separate the names in the path.
Note A 
package statement must be the first statement listed within the Java 
source. however, a comment or Javadoc comment may be written before the 
package statement.
An application can consist of any number of packages. If the widget application 
contains a few classes representing widget objects, they could be placed within the 
org.javabook.widget package. The application may have interfaces that interact with 
the widget objects. In this case, a package named org.javabook.interfaces may also 
contain any such interfaces.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   15   16   17   18   19   20   21   22   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling