In Java, for the convenience of organizing code, you can put functionally similar classes into a folder, this folder is called a package.
A package can contain not only classes, but also interfaces and other packages.
The directory is "\" to represent hierarchical relationships, such as E:\Java\workspace\Demo\bin\p1\p2\Test.java.
Package with "." To represent a hierarchical relationship, for example, P1.p2.Test represents a directory of \p1\p2\test.class.
How to implement a package
The package keyword allows you to declare a packet, for example:
Package p1.p2;
You must place the package statement in front of all statements, for example:
- Package P1. p2;
- Public class Test {
- public Test(){
- System. Out. println("I am the method of construction of the test class");
- }
- }
Indicates that the Test class is in the P1.P2 package.
Call of the package
In Java, there are two ways to invoke classes in other packages.
1) Precede each class name with the full package name
Examples of programs:
- Public class Demo {
- public static void main(String[] args) {
- Java. util. Date Today=new java. util. Date();
- System. Out. Println(today);
- }
- }
Operation Result:
Wed Dec 11:20:13 CST 2014
2) Introduce the class in the package via the import statement
Examples of programs:
- Import Java. util. Date;
- You can also introduce all the classes in the Java.util package
- Import java.util.*;
- Public class Demo {
- public static void main(String[] args) {
- Date Today=new date();
- System. Out. Println(today);
- }
- }
The result of the operation is the same as above.
In practical programming, it is not necessary to write the class to be introduced in detail, you can directly introduce all the classes in a particular package, such as import java.util.*;.
The path of the class
When you import a class, Java must know the absolute path to the class.
First create the Demo.java under the E:\Java\workspace\Demo\src\p0\ directory (E:\Java\workspace\Demo\src\ is the root of the project source file) and enter the following code:
- Package P0;
- Import P1. p2. Test;
- Public class Demo{
- public static void main(String[] args){
- Test obj = new test();
- }
- }
Then create the Test.java in the E:\JAVA\WORKSPACE\DEMO\SRC\P1\P2 directory and enter the following code:
- package p1.p2
-
- public class test {
- public test () {
- system.outprintln ( "I am the constructor of the test class"
- }
- }
Suppose we set the CLASSPATH environment variable to.;D: \ Program Files\jdk1.7.0_71\lib, source file Demo.java beginning with import p1.p2.Test; Statement, the compiler checks to see if there is a Test.java or Test.class file in the E:\Java\workspace\Demo\src\p0\p1\p2\ directory and continues to retrieve D:\Program if it does not exist files\ Jdk1.7.0_71\lib\p1\p2\ directory, the two directory does not exist will be an error. Obviously, Test.java is located in the E:\Java\workspace\Demo\src\p1\p2\ directory, compiler can not find, will error, how to do?
You can specify the Classpath through the classpath option of the Javac command.
Open cmd, enter the directory where the Demo.java file is located, execute the JAVAC command, and set the classpath to E:\Java\workspace\Demo\src Www.xingcaiyule.net as shown in:
When running a Java program, you also need to know the absolute path of the class, in addition to the path specified by the CLASSPATH environment variable, you can also increase the path through the Java command's classpath option, as shown in:
Note the difference between the Java command and the Javac command, the execution of the Javac command needs to enter the current directory, and the Execute Java command needs to go to the parent directory of the current directory, and the class name is preceded by the package name.
It can be understood that Javac is a platform command that operates on a specific platform file, indicating the path to the file being compiled. And Java is a virtual machine command, it on the class operation, that is, the description of the class to be in the form of a point of description, and can not add extension, but also note the case of the class name.
These commands are cumbersome, and actual development requires eclipse, which is handy for managing the package under Eclipse and compiling the run program. Eclipse is actually executing these commands.
Access rights for packages
A class, method, or member variable that is declared public can be used by any class under any package, and a class, method, or member variable declared as private can only be used by this class.
Classes, methods, and member variables that do not have any modifiers can only be accessed by all classes in this package and cannot be accessed by any class other than the package.
Three. Java classes and Objects 8. Talk about Java Packages