to better organize classes, Java provides a package mechanism for distinguishing the namespace of class names. Java Usage Packages (package) This mechanism is designed to prevent naming conflicts, access control, and provide search and positioning classes (class), interfaces, enumerations (enumerations), and annotations (annotation). Let's take a look at its role:
1, the function of similar or related classes or interfaces in the same package, easy to find and use the class.
2, like a folder, the package also uses a tree-shaped directory to store the way. The class names in the same package are different, and the names of the classes in the different packages can be the same, and when you call a class with the same class name in two different packages, you should add the package name to distinguish it. Therefore, packages can avoid name collisions.
3, the package also defines access rights, the class that has the package access permission can access the class in a package.
look at its grammatical format:
Package Pkg1 [. PKG2 [. Pkg3 ...] ] ;
For example, a Something.java file has its contents:
Package net.java.util;
public class something{
...
}
then its path should be Net/java/util/something.java so saved. The function of package (package) is to classify and save different Java programs, which is more convenient to be called by other Java programs. A package (package) can be defined as a set of interrelated types (classes, interfaces, enumerations, and annotations) that provide access protection and namespace management capabilities for these types. Here are some of the packages in Java:
Java.lang-Packaging base class java.io-functions that contain input and output functions
Developers can pack their own sets of classes and interfaces, and define their own packages. And it's worth advocating in practice, that when you're done with your class, grouping related classes can make it easier for other programmers to determine which classes, interfaces, enumerations, and annotations are relevant. Because the package creates a new namespace (namespace), it does not produce a naming conflict with any of the names in other packages. Using the package mechanism makes it easier to implement access control and make locating related classes simpler.
after you know the basics, we try to create and use it. When creating a package, you need to give the package a proper name. Then, if another source file contains the class, interface, enumeration, or annotation type provided by the package, you must place the package's declaration at the beginning of the source file. The package declaration should be in the first row of the source file, each source file can have only one package declaration, and each type in the file should be used for it. If a package declaration is not used in a source file, the class, function, enumeration, annotation, etc. will be placed in an unnamed package (unnamed package). Let's take a look at an example that creates a package called animals. It is usually used to name lowercase letters to avoid conflicts with class or interface names. First, add an interface (interface) to the animals package:
/* FileName: Animal.java * *
Package animals;
Interface Animal {
public void eat ();
public void travel ();
}
Next, add the implementation of the interface in the same package:
Package animals;
/* FileName: Mammalint.java * *
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 ();
}
}
then, compile the two files and put them in a subdirectory called animals. Run with the following command:
$ mkdir Animals
$ cp animal.class mammalint.class animals
$ java animals/mammalint
mammal
eats Mammal Travel
In order to be able to use a member of a package, we need to explicitly import the package in the Java program. Use the "Import" statement to complete this function. After the import statement in the Java source file should be located in the package statement, the definition of all classes can be either without or multiple, with the syntax in the form:
Import package1 [. Package2 ...]. (classname |*);
If a class in a package wants to use another class in this package, the package name can be omitted. The following payroll package already contains the Employee class, then add a Boss class to the payroll package. Boss class refers to the Employee class can not use the payroll prefix, Boss class examples are as follows (boss. java file):
Package payroll;
public class Boss
{
public void Payemployee (Employee e)
{
E.mailcheck ();
}
}
What if the Boss class is not in the payroll package. The Boss class must use one of the following methods to refer to classes in other packages. Use the class full name description, for example:
Payroll. Employee
introduced with the import keyword, using the wildcard character "*":
Import Payroll. *;
To introduce an Employee class using the import keyword:
Import Payroll. Employee;
However, we should note that the class file can contain any number of import declarations. The import declaration must precede the package declaration, before the class declaration.
here, we are familiar with the directory structure of the next package. There are two main consequences of a class being placed in a package:
The package name becomes part of the class name, as we discussed earlier. The package name must match the directory structure where the corresponding byte code resides.
Here's a simple way to manage your own files in Java. First, the source of the class, interface, and so on, is placed in a text, and the name of the file is the name of the type, with the. java extension. For example:
FileName: Car.java
Package vehicle;
public class Car {
class implements
}
Next, place the source file in a directory that corresponds to the name of the package where the class resides:
... \ vehicle \ Car. Java
right now, the correct class name and path will look like the following:
Class name-> vehicle. Car
Path name-> Vehicle\car.java (in Windows system)
Typically, a company uses its Internet domain name upside down form as its package name. For example: The Internet domain name is luyaran.com, all package names begin with Com.lytaran. Each part of the package name corresponds to a subdirectory. For example: There is a com.luyaran.test package that contains a source file called Luyaran.java, and accordingly, it should be like the following series of subdirectories:
... \ com \ Luyaran \ Test \ Luyaran . Java
At compile time, the compiler creates a different output file for each type of class, interface, etc. defined in the package, and the name of the output file is the name of this type, plus. class as the extension suffix. For example:
FileName: Luyaran.java
Package com.lyaran.test;
public class Luyaran {
}
Class Google {
}
Now, we compile this file with the-D option, as follows:
$javac-D. Luyaran.java
This will place the compiled file as follows:
. \com\luyaran\test\luyaran.class
. \com\luyaran\test\google.class
You can import the classes, interfaces, and so on that are defined in all \com\luyaran\test\ as follows:
Import com.luyaran.test.*;
The. class files after compilation should be the same as the. Java source files, and the directories they place should correspond to the names of the Chongbao. However, the path to the. class file is not required to be the same as the corresponding. Java path. You can separate the source and the class directory.
<path-one>\sources\com\luyaran\test\luyaran.java
<path-two>\classes\com\luyaran\test\ Google.class
this way, you can share your class directory with other programmers without revealing your source code. Managing Source and class files in this way allows compilers and Java virtual machines (JVMS) to find all types used in your program. The absolute path of the class directory is called class path. Set in the system variable CLASSPATH . Compilers and Java Virtual machines later construct the path to the. class file by adding the package name to the class path. <path-two>\classes is the class Path,package name is Com.luyaran.test, and the compiler and JVM will be in <path-two>\classes\com\luyaran\ Find the. class file in test. A class path may contain several paths, and multiple paths should be separated by delimiters. By default, the compiler and JVM look for the current directory. JAR files are based on classes that contain Java platforms, so their directories are placed by default in class path.
Now that we've talked about this, let's see how to set the CLASSPATH system variable using the DOS command. We can use the following command to display the current CLASSPATH variable:
Windows platform (under DOS command line):c:\> set CLASSPATH UNIX platform (under Bourne shell): # echo $CLASSPATH
Delete current Classpath variable contents:
Windows platform (under DOS command line):c:\> set classpath= UNIX platform (under Bourne shell): # unset CLASSPATH; Export CLASSPATH
To set the CLASSPATH variable:
Windows platform (under DOS command line): c:\> set classpath=c:\users\jack\java\classes UNIX platform (under Bourne shell): # classpath=/home/jack/ java/classes; Export CLASSPATH Well, everything about the bag has been recorded here. If the feeling is good, please give a lot of praise support oh ...