We have written some Java programs. Every previous Java program was saved as a file, such as Test.java. Subsequently, the program is compiled into Test.class. We end up using $java Test to run the program.
However, in a normal Java project, we often need to write more than one .java program, and the final Java product includes all Java programs. Therefore, Java needs to solve the problem of organizing Java programs. The purpose of a package is to better organize Java programs.
Package creation
Package creation is very simple. We only need to add the package at the beginning of the Java program. Let's take the Human class as an example and put it into the package:
Copy the code code as follows:
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 code for copying the first line of statements above is as follows:
package com.vamei.society;
Indicates that the program is in the com.vamei.society package. com.vamei (the reverse of vamei.com) represents the domain name of the package author (unfortunately, this domain name has been registered by someone else, so it is only used for demonstration purposes). Java requires packages to have domain name prefixes to distinguish different authors. Society is a further local path name. com.vamei.society together form the name of the package.
Packages provide a name space for Java programs. The full 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 identified by their full paths, so you can have classes with the same name in different packages without Java getting confused. For example, com.vamei.society.Human and com.vamei.creature.Human are two different classes.
Let’s look at one more detail. The Human class is public, and its constructor is also public, so any other object can call this class. As we said before, there can only be one public class in a Java file, and the class must have the same name as the .java file. A class does not need the public keyword, which actually represents a permission: the class is visible in the package in which it is located. That is, other Java programs in the package can access the class. This is the default access in Java.
Likewise, members of an object can also have default permissions (visible in packages). For example, we remove the public keyword in front of 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 be automatically compiled when used.
If the entire package (that is, the com folder) is located in the current working path, then no special settings are required to use the package, such as the following TestAgain.java:
Copy the code code as follows:
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 under the corresponding path. *Indicates the introduction of all classes in the society folder. In TestAgain, we use the Human class directly.
We can also provide the full path to the class. This can distinguish between classes with the same name but different paths, such as:
Copy the code code as follows:
public class TestAgain
{
public static void main(String[] args)
{
com.vamei.society.Human aPerson =
new com.vamei.society.Human(180);
System.out.println(aPerson.getHeight());
}
}
Since we provide the complete classpath, there is no need to use the import statement.
If the package is not placed 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 Human.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 specify the folder path where the package is located when using javac and java, for example:
Copy the code code as follows:
$javac -classpath /home/vamei/javapackage:. TestAgain.java
$java -classpath /home/vamei/javapackage:. TestAgain
Just look for the package from /home/vamei/javapackage and the working path (.). Java can find the Human class from /home/vamei/javapackage and the TestAgain class from .
In addition, you can also set the system's CLASSPATH environment variable and add the above path to the variable without typing the -classpath option every time.
Mechanisms similar to packages are also common in other languages, such as the import mechanism in Python. They are all about better organizing and using existing programs. Using packages, we can easily expand Java programs and use existing Java program libraries. Notice that the package manages .class files. Java is known as "Compile Once, run anywhere" (Compile Once, run anywhere). .class files can run on any platform equipped with a Java Virtual Machine (JVM, Java Virtual Machine), which helps us overcome program porting difficulties caused by system differences.
The differences between systems can be very large. If we write a program in C language, the source program needs to be recompiled on each platform to adapt to different hardware conditions. The Java virtual machine connects the platform and the Java universe, and it forms the middle layer between hardware and programming logic. The JVM hides hardware differences and provides programmers with a "standard" Java universe. The .class file can be regarded as the currency circulating in this Java universe. With the JVM infrastructure and package management assistance, Java programs achieve good portability.
Summarize
package, import
Default permissions: Visible in package
-classpath, CLASSPATH