We've written some Java programs. Each of the previous Java programs was saved as a file, such as Test.java. Subsequently, the program is compiled to 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, the final Java product includes all Java programs. Therefore, Java needs to solve the problem of organizing Java programs. The purpose of the package (package) is to better organize Java programs.
The establishment of the package
The establishment of the package is very simple. We'll just add package to the beginning of the Java program. Let's take the human class as an example and put it in the package:
Copy 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 first line of statements above
Copy Code code as follows:
Package com.vamei.society;
Indicates that the program is in the Com.vamei.society package. Com.vamei (vamei.com) represents the name of the package author (unfortunately, this domain name has been registered, so it only acts as a demo). Java requires that the package have a domain prefix to differentiate between authors. Society is a further local path name. Com.vamei.society together form the name of the package.
The package provides a namespace (name space) for the Java program. The full path of a Java class is composed 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 the full path, so there can be classes with the same name in different packages, and Java is not confused. For example, Com.vamei.society.Human and Com.vamei.creature.Human are two different classes.
Look at one more detail. The human class is public and its construction method is public, so any other object can call the class. As we said before, a Java file can have only one class that is public, and that class is going to have the same name as the. java file. A class can have no public keyword, and it actually represents a permission: The class is visible in the package in which it resides. That is, other Java programs in the package can access the class. This is the default access permission in Java.
Similarly, the members of an object can also be default permissions (visible in the package). For example, we remove the public keyword in front of the GetHeight () method.
The call to the package
We just need to put the Human.java compiled human.class into the appropriate folder. For example, I put Human.class into the com/vamei/society/. In fact, you can also put the. java file into the appropriate path, Java will be automatically compiled when used.
If the entire package (that is, the COM folder) is in the current work path, you do not need special settings to use the package, such as the following testagain.java:
Copy 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. * Represents the introduction of all classes under the Society folder. In Testagain, we use the human class directly.
We can also provide a complete path to the class. This distinguishes between classes that have the same name but different paths, such as:
Copy 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 ());
}
}
Because we provide a complete classpath, we do not need to use the import statement.
If the package is not placed under the current working path, we need to notify Java when we use the package. For example, we put the package in the/home/vamei/javapackage so that Human.class is located in/home/vamei/javapackage/com/vamei/society/ Human.class, and our work path is/home/vamei. This way, the package cannot be found. One way to do this is to use-classpath to describe the folder path where the package resides, such as: Javac and Java.
Copy Code code as follows:
$javac-classpath/home/vamei/javapackage:. Testagain.java
$java-classpath/home/vamei/javapackage:. Testagain
is from the/home/vamei/javapackage and the work path (.) To find the package. Java can find the human class from the/home/vamei/javapackage, where the Testagain class can be found.
Alternatively, you can set the system's CLASSPATH environment variable and add the above path to the variable without typing the-classpath option each time.
Packet-like mechanisms are also common in other languages, such as the import mechanism in Python. They are all for better organization and use of existing programs. With packages, we can easily expand Java programs and use existing Java libraries. Note that the package manages the. class file. Java is known as "compile all at Once" (Compile Once, run anywhere). The. class file can be run on any platform containing Java virtual machines (JVM, Java, Machine). This helps us overcome the problem of program porting caused by system differences.
The differences between systems can be very large. If we write programs in C language, we need to recompile the source program on each platform to adapt to different hardware conditions. The Java Virtual machine joins the platform and the Java universe, which form the middle tier of hardware and programming logic. The JVM hides hardware differences and provides programmers with a "standard" Java universe. The. class file can be viewed as a currency in circulation in the Java universe. Under the JVM's infrastructure, and with package management assistance, Java programs are well Portable (portability).
Summarize
Package, Import
Default permissions: Visible in Package
-classpath, Classpath