Packages
Packages
Store classes
How to store classes in a package
- The classes in a package should be saved in a subdirectory with a path that matches the name of the package.
- The package name should be unique so you don't have conflicts with other packages.
- To include a class in a package, code a package statement as the first statement in the file that defines the class. Then, include an import statement in any file that uses the package.
- You can use packages to organize the classes that make up an application. In that case, you should store the subdirectories that make up the package beneath the directory for the application.
- You can also use packages to make it easy to reuse a set of classes. In that case, you should store the subdirectories that make up the package in a central location, such as in the java directory.
The directory structure for an application that uses packages
lineItem LineItemApp.java murach business Product.java LineItem.java database ProductDB.java presentation Validator.java
The LineItem class in the package
package murach.business; import java.text.NumberFormat; import murach.business.Product; public class LineItem{...}
The Product class
package murach.business; import java.text.NumberFormat; public class Product{...}
The ProductDB class in the package
package murach.database; import murach.business.Product; public class ProductDB{...}
The Validator class
package murach.presentation; import java.util.Scanner; public class Validator{...}