We have written some Java programs. Each previous Java program is saved as a file, such as Test. java. Then, the program is compiled as Test. class. We finally use $ java Test to run the program.
However, in a normal Java project, we often need to write more than one. java program. The final Java product includes all Java programs. Therefore, Java needs to solve the problem of organizing Java programs. Package aims to better organize Java programs.
Package Creation
Package creation is very simple. We only need to add a package at the beginning of the Java program. Let's take the Human class as an example and put it into the package:
Package com. vamei. society;
Public class Human
{
/**
* Constructor
*/
Public Human (int h)
{
This. height = h;
System. out. println ("I'm born ");
}
/**
* Accessor
*/
Public int getHeight ()
{
Return this. height;
}
/**
* Mutator
*/
Public void growHeight (int h)
{
This. height = this. height + h;
}
Private int height;
}
The first line of the preceding statement
Package com. vamei. society;
Indicates that the program is in the com. vamei. society package. Com. vamei (vamei.com anti-write) indicates the Domain Name of the package author (unfortunately, this domain name has been registered by someone else, so it only serves as a demo ). Java requires that the package have a domain name prefix to distinguish different authors. Society is the further local path name. Com. vamei. society compose the package name.
The package provides a namespace for the Java program ). The complete path of a Java class consists of its package and class name, such as com. vamei. society. Human. The corresponding Human. java program should be placed under com/vamei/society. Classes are completely identified by paths, so different packages can have classes with the same name, and Java will not be confused. For example, com. vamei. society. Human and com. vamei. creature. Human are two different classes.
Let's look at the details. The Human class is public and its constructor is public, so any other object can call this class. As we have said before, a Java program can only have one public class, which must go to the. java file with the same name. A class can have no public keyword, and it actually represents a permission: the class is visible in its package. That is, other Java programs in the package can access this class. This is the default access permission in Java.
Likewise, the object member can be the default permission (visible in the package ). For example, we remove the public keyword before the getHeight () method.
Package call
We only need to put the Human. class compiled by Human. java into the corresponding folder. For example, I put Human. class into com/vamei/society. In fact, you can also put the. java file into the corresponding path, and Java will automatically compile during use.
If the entire package (that is, the com folder) is located in the current working path, you can use the package without special settings, such as TestAgain. java below:
Import com. vamei. society .*;
Public class TestAgain
{
Public static void main (String [] args)
{
Human aPerson = new Human (180 );
System. out. println (aPerson. getHeight ());
}
} Import is used to identify paths. Using the import Statement, we can introduce classes in the corresponding path. * Introduces all classes in the society folder. In TestAgain, we directly use the Human class.
We can also provide the complete path of the class. This can distinguish classes with the same name but different paths, such:
Public class TestAgain
{
Public static void main (String [] args)
{
Com. vamei. society. Human aPerson =
New com. vamei. society. Humans (180 );
System. out. println (aPerson. getHeight ());
}
} Because we provide a complete class path, you do not need to use the import Statement.
If the package is not in the current working path, we need to notify Java when using the package. For example, we put the package in/home/vamei/javapackage, so that the Human. the class is located in/home/vamei/javapackage/com/vamei/society/Human. class, and our working path is/home/vamei. In this way, the package cannot be found. One method is to use-classpath to describe the path of the folder where the package is located when javac and java are used. For example:
$ Javac-classpath/home/vamei/javapackage:. TestAgain. java
$ Java-classpath/home/vamei/javapackage:. TestAgain
Find the package from/home/vamei/javapackage and working path. Java can find the Human class from/home/vamei/javapackage, and TestAgain class from.
You can also set the CLASSPATH environment variable of the system to add the above path to the variable, instead of entering the-classpath option every time.
Similar to the package mechanism is also common in other languages, such as the import mechanism in Python. They are all for better organization and use of existing programs. Using the package, we can easily expand the Java program and use the existing Java library. Note that the package manages the. class file. Java is called "One-time compilation, run everywhere" (Compile Once, run anywhere ).. Class files can run on any platform with a Java Virtual Machine (JVM) installed. This helps us overcome the difficulty of program porting caused by system differences.
The differences between systems can be very large. If we use C language to write programs, we need to re-compile the source program on each platform to adapt to different hardware conditions. The Java Virtual Machine connects the platform and the Java universe. It forms the middle layer of hardware and programming logic. JVM hides hardware differences and provides programmers with a "standard" Java universe. The. class file can be seen as the currency circulating in the Java universe. With the help of package management in the infrastructure of JVM, Java programs achieve good portability ).