Java package
In order to better organize classes, Java provides a package mechanism to distinguish the namespace of class names.
The role of packages
#1 Organize classes or interfaces with similar or related functions into the same package to facilitate the search and use of classes .
2 Like folders, packages are also stored in a tree directory. The names of classes in the same package are different, and the names of classes in different packages can be the same. When classes with the same class name in two different packages are called at the same time, the package name should be added to distinguish them. Therefore, packages can avoid name conflicts.
3 Packages also limit access permissions. Only classes with package access permissions can access classes in a certain package.
Java uses the package mechanism to prevent naming conflicts, access control, and provide search and location for classes, interfaces, enumerations, and annotations )wait.
The syntax format of the package statement is:
package pkg1[.pkg2[.pkg3…]];
For example, the content of a Something.java file
package net.java.util public class Something{ ... }
then its path should be net/java/util/Something .java is saved like this. The function of package is to classify and save different Java programs so that they can be called by other Java programs more conveniently.
A package can be defined as a set of interconnected types (classes, interfaces, enumerations and annotations), providing access protection and namespace management functions for these types.
The following are some packages in Java:
java.lang-packaging basic classes
java.io- Functions containing input and output functions
Developers can package a set of classes and interfaces by themselves and define their own package. And it is worth advocating to do this in actual development. After you complete the implementation of the class yourself, grouping related classes can make it easier for other programmers to determine which classes, interfaces, enumerations, annotations, etc. are related. .
Because the package creates a new namespace (namespace), it will not cause naming conflicts with any names in other packages. Using the package mechanism makes it easier to implement access control and makes it easier to locate related classes.
Creating a package
When creating a package, you need to give the package an appropriate name. Later, if another source file contains classes, interfaces, enumerations or annotation types provided by this package, the package declaration must be placed at the beginning of the source file.
The package declaration should be on the first line of the source file. There can be only one package declaration per source file, and every type in this file applies to it.
If no package declaration is used in a source file, the classes, functions, enumerations, annotations, etc. will be placed in an unnamed package.
Example
Let's look at an example that creates a package called animals. Usually use lowercase letters to avoid conflicts with class and interface names.
Add an interface to the animals package:
/* 文件名: Animal.java */ package animals; interface Animal { public void eat(); public void travel(); }
Next, add the implementation of the interface to the same package:
package animals; /* 文件名 : 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 these two files and place them in a subdirectory called animals. Use the following command to run:
$ mkdir animals $ cp Animal.class MammalInt.class animals $ java animals/MammalInt Mammal eats Mammal travel
import keyword
In order to use the members of a certain package, we need to explicitly import the package in the Java program. This function can be accomplished using the "import" statement.
In the java source file, the import statement should be located after the package statement and before the definitions of all classes. There may be none or multiple statements. The syntax format is:
import package1[.package2…].(classname|*);
If it is in a package If a class wants to use another class in this package, the package name can be omitted.
Example
The following payroll package already contains the Employee class. Next, add a Boss class to the payroll package. The Boss class does not need to use the payroll prefix when referencing the Employee class. Examples of the Boss class are as follows.
package payroll; public class Boss { public void payEmployee(Employee e) { e.mailCheck(); } }
What happens if the Boss class is not in the payroll package? The Boss class must use one of the following methods to reference classes in other packages
Use the full name of the class to describe, for example:
payroll.Employee
Use the import keyword to introduce, use the wildcard "*"
import payroll.*;
Use the import keyword to introduce the Employee class
import payroll.Employee;
Note:
The class file can contain any number of import statements. The import statement must come after the package declaration and before the class declaration.
Directory structure of package
There will be two main results when a class is 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 bytecode is located.
The following is a simple way to manage your own files in Java:
Put the source code of classes, interfaces, etc. in a text, the name of this file is the name of this type, with .java as the extension. For example:
// 文件名 : Car.java package vehicle; public class Car { // 类实现 }
Next, place the source file in a directory that corresponds to the name of the package where the class is located.
....\vehicle\Car.java
Now, the correct class name and path will look like this:
Class name-> vehicle.Car
Pathname-> vehicle\Car.java (in windows)
Usually, a company uses the reverse form of its Internet domain name as its package name. For example: Internet domain name It's apple.com, and all package names start with com.apple. Each part of the package name corresponds to a subdirectory.
For example: This company has a com.apple.computers package, which contains a source file called Dell.java. Correspondingly, there should be a series of subdirectories as follows:
....\com\apple\computers\Dell.java
When compiling, the compiler creates a different output file for each class, interface, etc. type defined in the package. The name of the output file is the name of this type, with .class as the extension suffix. For example:
// 文件名: Dell.java package com.apple.computers; public class Dell{ } class Ups{ }
Now, we use the -d option to compile this file as follows:
$javac -d . Dell.java
This will place the compiled file as follows:
.\com\apple\computers\Dell.class.\com\apple\computers\Ups.class
You can import all classes, interfaces, etc. defined in \com\apple\computers\ as follows:
import com.apple.computers.*;
The compiled .class file should be the same as the .java source file. They are placed The directory should correspond to the name of the package. However, it is not required that the path of the .class file is the same as the path of the corresponding .java. You can arrange source and class directories separately.
<path-one>\sources\com\apple\computers\Dell.java <path-two>\classes\com\apple\computers\Dell.class
In this way, you can share your class directory with other programmers without revealing your source code. Managing source code and class files in this way allows the compiler and Java Virtual Machine (JVM) to find all types used in your program.
The absolute path to the class directory is called class path. Set in the system variable CLASSPATH. The compiler and Java virtual machine construct the path to the .class file by adding the package name to the class path.
<path- two>\classes is the class path, the package name is com.apple.computers, and the compiler and JVM will be found in <path-two>\classes\com\apple\compters .class file.
A class path may contain several paths. Multiple paths should be separated by delimiters. By default, the compiler and JVM look in the current directory. JAR files contain classes related to the Java platform, so their directories are placed in the class path by default.
Set CLASSPATH system variable
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 the contents of the current CLASSPATH variable:
- Windows platform (under DOS command line) -> C:\> set CLASSPATH=
- UNIX platform (under Bourne shell) -> % unset CLASSPATH; export CLASSPATH
- ##
Windows platform (under DOS command line)-> set CLASSPATH=C:\users\jack\java\classes
- ## UNIX platform (under Bourne shell)-> % CLASSPATH=/home/jack/java/classes; export CLASSPATH