This information comes from runoob , slightly modified.
In order to better organize the class, Java provides a package mechanism for the difference between the class name of the namespace.
Java uses package (package) This mechanism is to prevent naming conflicts, access control, providing search and locate class (class), interfaces, enumerations (enumerations) and comments (annotation) and so on.
Role package
1, the functionally similar or related tissue class or interface in the same package, a class to find and easy to use.
2, as in the same folder, the package also used storage directory tree. Classes in the same package name is different, the name of the class in a different package is the same, while simultaneously calling two different classes in the same package name of class, distinction should be added to the package name. Therefore, the package name to avoid conflict.
3, the package also defines access rights, have access to the package of the class can access a package of classes.
Syntax package
package pkg1[.pkg2[.pkg3…]];
For example, a file Something.java its contents
package net.java.util; public class Something{ ... }
Then its path should be net / java / util / Something.java so saved.
Action package (package) is to classify the different java program saved more easily be called by other java program.
A package (package) can be defined as a set of types (classes, interfaces, enumerations, and annotation) interconnected , providing access protection and namespace management for these types of functions.
The following are some of the Java package:
- the java.lang - package basis of class
- the java.io - function of the input and output functions comprising
Developers can own a set of classes and interfaces package, and define their own package. And to do so in the actual development is to be encouraged, and when you complete the implementation of the class, grouping related classes, so that other programmers can more easily determine which classes, interfaces, enumerations, and comments are related .
Because the package creates a new namespace (namespace), so without naming conflicts with the name of any other packages. Use package this mechanism, easier to implement access control, and let locate the relevant class easier.
Creating a Package
Typically use lowercase letters to avoid naming classes, interfaces names of conflict.
Example: create a package called the animals.
animals added to the packet interface (interface):
/* 文件名: Animal.java */ package animals; interface Animal { public void eat(); public void travel(); }
Add implement the interface in the same package
/ * File Name: MammalInt.java * /
Package Penalty for Animals;
public class MammalInt implements Animal{ public void eat(){ System.out.println("Mammal eats"); } public void travel(){ System.out.println("Mammal travels"); } public int noOfLegs(){ return 0; } public static void main(String args[]){ MammalInt m = new MammalInt(); m.eat(); m.travel(); } }
Compile
These two documents, put them in a subdirectory named animals.
$ mkdir animals $ cp Animal.class MammalInt.class animals $ java animals/MammalInt Mammal eats Mammal travel
import
Use a member of a package, we need to "import" statement explicitly import the package.
import declaration must be after the package statement, class declaration before.
grammar
import package1[.package2…].(classname|*);
example
payroll package already contains Employee class, then added to a class Boss payroll package. When Boss class reference the Employee class can not use payroll prefix.
If the class is not Boss payroll package, the following reference Boss class other classes in the package.
The full name of the class description
payroll.Employee
* the introduction of import using wildcards
import payroll.*;
the introduction of direct import
import payroll.Employee;
package directory structure
Based on the package there are two main results: package name becomes part of the class name; the name of the package directory structure must be located under the corresponding byte codes match.
Simple way
The type of class, interface source code in a text, the name of this file is the name of this type, and to .java extension.
// File Name: Car.java Package Vehicle; public class Car { // class implementation }
Next, the source files in a directory, the directory where the class name corresponds to the package.
....\vehicle\Car.java
Class name and path will be
-
Class Name: vehicle.Car
-
Pathname: vehicle \ Car.java (windows in the system)
Common way
Typically, a company uses its reversed form of Internet domain names to its package names such as: Internet domain name is runoob.com, all the package names are beginning to com.runoob. Each portion of the packet corresponding to a subdirectory name.
For example: There is a com.runoob.test package, this package contains the source file called Runoob.java, then the appropriate, should be like following a series of subdirectories:
.... \ com \ runoob \ test \ Runoob.java
Compiler package
Compile time, the compiler creates a different output file, the output file name is the name of this type is the type of each class, interface defined in the package, plus as an extension .class suffix. E.g:
// File name: Runoob.java Package Penalty for com.runoob.test; public class Runoob { } class Google { }
Now, we use the -d option to compile the file as follows:
// a package file when compiling
$ javac -d. Runoob.java
compile time // only one file
javac A.java
Run: will generate their own folder after compiling, do not go into that folder directly run java -cp / home / test test.Run, where the source file in the test folder called test pack, the startup file is Run.java .
This will place the compiled file such as the following:
. \ com \ runoob \ test \ Runoob. class . \ com \ runoob \ test \ Google. class
Import All \ com \ runoob \ test \ exemplary defined classes, interfaces, etc.
import com.runoob.test.*;
<path-one>\sources\com\runoob\test\Runoob.java <path-two>\classes\com\runoob\test\Google.class
In this way, you can share your classes directory to other programmers without having to disclose their source.
In this way manage their source code and class files can let the compiler and the java virtual machine (JVM) can find all types used in your program.
class path
Absolute path to the directory category called class path . Set the system variable CLASSPATH in.
Compiler and the java virtual machine by the package name to the class path path to construct the .class file.
<Path- two> \ classes is the class path, package name is com.runoob.test, and the compiler and JVM will \ classes \ \ runoob \ test to find the .class files in <path-two> com.
A class path might contain several paths, multi-path should be separated by a delimiter.
By default, the compiler and JVM look in the current directory . JAR file contains the class by the Java platform dependent, so they default on a directory in the class path.