The following is referenced from http://wiki.jikexueyuan.com/project/java/packages.html:
The use of packages in Java is to prevent naming conflicts and to control access, making it easier to use search/locate and classes, interfaces, enumerations, and annotations.
A package can be defined as a set of related types (classes, interfaces, enumerations, and annotations) that provide access protection and namespace management.
Some of the packages that already exist in Java are:
java.lang-Contains the basic classes
java.io-Classes with input and output functions
Programmers can define their own packages to contain a variety of classes and interfaces. It is a good practice to organize the related classes of implementations so that a programmer can easily know which classes, interfaces, enumerations, and annotations are relevant.
Because the package creates a new namespace, it will not conflict with names in other packages. With packages, you can easily provide access control, and you can easily navigate to related classes.
First, create the package
When creating a package, a name should be given to the package, and the package declaration of the name is placed at the top of each source file that contains classes, interfaces, enumerations, and annotation types.
packageThe declaration statement should be placed on the first line of the source file. There can be only one package declaration statement in each source file, and it applies to all types of files.
If the package declaration is not used, then the class, interface, enumeration, and annotation types will be put into a nameless package.
Example:
An example of a Animal package called is created. It is customary to use a lowercase name package to avoid conflicts with class, interface names.
Declare an interface in package animals:
/** * package animals; Interface Animal { publicvoid eat (); Public void Travel ();}
Now, implement a class in the same package animals:
Packageanimals;/*File Name:MammalInt.java*/ Public classMammalintImplementsanimal{ Public voideat () {System.out.println ("Mammal Eats"); } Public voidTravel () {System.out.println ("Mammal Travels"); } Public intNooflegs () {return0; } Public Static voidMain (String args[]) {mammalint m=NewMammalint (); M.eat (); M.travel (); }}
Now, you can compile these two files and place them in a subfolder called animals subfolders, and then run as follows:
$ mkdir animals$ cp Animal. class Mammalint. class animals$ java Animals/mammalintmammal Eatsmammal travels
Second, import keywords
If a class wants to use another class in the same package, it is not necessary to use the package name. Classes in the same package can be identified with each other without any special syntax.
Example:
A class called Boss is added to the payroll package that already contains the employee. In the case where the payroll prefix is not used, the Boss class refers to the employee class, as shown in the following boss class:
Package Payroll; Public class boss{ publicvoid payemployee (Employee e) { e.mailcheck (); }}
What happens if the Boss class is not in the payroll package? Then the boss class must use one of the following methods to refer to classes in other packages.
You can use the full name. For example:
Payroll. Employee
You can import by using the Import keyword and the wildcard character (*). For example:
import payroll.*;
You can import the class itself by using the Import keyword. For example:
Import Payroll. Employee;
Note: A file of a class can contain any number of import statements. These import statements must precede the package declaration statement and the class's definition statement.
Third, the directory structure of the package
The following two results occur primarily when a class is placed in a package:
- The name of the package becomes part of the class name.
- The name of the package must match the directory structure of the associated bytecode file.
In Java, there is a simple way to manage files:
The source code for classes, interfaces, enumerations, and annotation types exists in a .java text file with an extension. For example:
// File Name: Car.java package vehicle; Public class Car { // Class implementation. }
Now, place the source file in a folder whose name reflects the name of the package:
..... \vehicle\car.java
Now, the correct class name and path name should look like this:
- Class name, vehicle. Car
- Path name, Vehicle\car.java (in Windows)
In general, a company uses its reverse-order Internet domain name as a package. For example: A company's Internet domain name is apple.com, then its package name should start with Com.apple. Each part of the package name reflects a subdirectory.
For example, a company that has a com.apple.computers package containing the Dell.java source file will be included in a series of subfolders like the following:
..... \com\apple\computers\dell.java
At compile time, the compiler creates a different output file for each class, interface, and enumeration that is defined in it. The base name of the output file is the name .class of the type and the extension.
For example:
// File Name:Dell.java Package com.apple.computers; Public class dell{} class ups{}
Now, use the-D option as below to compile the file:
$javac-D. Dell.java
This will store the compiled file as follows:
. \com\apple\computers\dell. class . \com\apple\computers\ups. class
You can import all of the classes and interfaces defined in \com\apple\computers\ as follows:
import com.apple.computers.*;
Like the. Java source files,. Class These compiled files should be in a series of folders that reflect the package name. However, the path to the. class file does not have the same path as the. Java source file. You can manage the source files and the compiled files directory independently, as follows:
<path-one>\sources\com\apple\computers\dell.java<path-two>\classes\com\apple\computers\ Dell. class
By doing this, you can give the compiled folder to other programmers without exposing the source files. It is also necessary to manage the source files and the compiled class files in such a way that the compiler and the Java Virtual Machine (JVM) can find all the types used in the program.
The full path of the compiled folder, \\classes, called Class path, is set in the system environment variable CLASSPATH. The compiler and the JVM construct the path to the. class file by adding the package name to the class path.
Say \\classes is class path, the package name is Com.apple.computers, and then the compiler and the JVM will look for the. class file in \\classes\com\apple\compters.
A class path may contain several paths. Multiple paths are separated by semicolons (Windows) or colon (Unix). By default, the compiler and the JVM look for the current directory and the jar files that contain the Java platform bytecode so that the folders are automatically in class path.
Iv. Setting the CLASSPATH environment variables in the system
In order to display the current CLASSPATH environment variables, use the following commands in Windows and UNIX (Bourne shell):
Set classpath in UNIX- Echo $CLASSPATH
Use the following command to delete the contents of the current CLASSPATH variable:
Set Classpath= in UNIX->% unset CLASSPATH; Export CLASSPATH
Use the following command to set the CLASSPATH variable:
Set Classpath=c:\users\jack\java\classes in UNIX->% classpath=/home/jack/java/classes; Export CLASSPATH
Packages in Java