Java package (package)

In order to better organize the classes, Java provides a package mechanism that distinguishes namespaces for class names.

The role of the package

  • 1. Organize functionally similar or related classes or interfaces in the same package to facilitate the search and use of classes.
  • 2, as the folder, the package also uses a tree directory storage. The names of the classes in the same package are different. The names of the classes in different packages can be the same. When two classes of the same class name in two different packages are called at the same time, the package names should be added to distinguish them. Therefore, packages can avoid name conflicts.
  • 3, the package also defines access rights, classes that have package access rights can access classes in a package.
Java uses packages as a mechanism to prevent naming conflicts, access control, searching and locating classes, interfaces, enumerations, and annotations.
The syntax of the package statement is:
package pkg1 [ . Pkg2 [ . Pkg3 ... ] ] ;
For example, a Something.java file its contents
package net . java . util ; public class Something { ... }
Then its path should be saved as net/java/util/Something.java . The role of the package is to save different java programs and make it more convenient for other java programs to call.
A package can be defined as a set of interconnected types (classes, interfaces, enumerations, and comments) that provide access protection and namespace management capabilities for these types.
The following are some of the packages in Java:
  • Java.lang - Packaged base class
  • Java.io - function containing input and output functions
Developers can package their own set of classes and interfaces, and define their own packages. Moreover, doing so in real development is worth advocating. When you complete the implementation of the class yourself, grouping related classes can make it easier for other programmers to determine which classes, interfaces, enumerations, and comments are relevant. .
Because the package creates a new namespace, it does not create naming conflicts with any of the names in other packages. Using this mechanism of packages, it is easier to implement access control and make it easier to locate related classes.

Create package

When creating a package, you need to take a suitable name for this package. Later, if one of the other source files contains the class, interface, enumeration, or comment type provided by the package, the package declaration must be placed at the beginning of the source file.
The package declaration should be in the first line of the source file, and there can be only one package declaration for each source file, and each type in this file is applied to it.
If a package declaration is not used in a source file, its classes, functions, enumerations, comments, etc. will be placed in an unnamed package.

example

Let's look at an example. This example creates a package called animals. Lowercase letters are often used for naming to avoid conflicts with classes and interface names.
Add an interface to the animals package:

Animal.java file code:

/* File name: Animal.java */
package animals; interface Animal { public void eat(); public void travel(); }
Next, add the interface's implementation in the same package:

MammalInt.java file code:

package animals ; /* File name: 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 place 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

Import keyword

In order to be able to use a member of a package, we need to explicitly import the package in a Java program. Use the "import" statement to accomplish this function.
The import statement in the java source file should be located after the package statement. Before the definition of all the classes, there may be no or more than one. The syntax is:
import package1 [ . package2 ... ] . ( classname |* ) ;
If a class wants to use another class in this package in a package, the package name can be omitted.

example

The following payroll package already contains the Employee class and then adds a Boss class to the payroll package. The Boss class can refer to the Employee class without using the payroll prefix. Boss class instances are as follows.

Boss.java file code:

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 reference classes in other packages.
Use the full name of the class description, for example:
payroll . Employee
Introduced with the import keyword using 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 declarations. The import declaration must follow the package declaration before the class declaration.

The directory structure of the package

There are two main results of the class in the package:
  • The package name becomes part of the class name, as we discussed earlier.
  • The package name must match the directory structure in which the corresponding bytecode is located.
Here is a simple way to manage your own java files:
Place the source code of a class, interface, etc. type in a text whose name is the name of this type and with .java extension. E.g:
// File Name: Car.java package vehicle ; public class Car {
// class implementation }
Next, place the source files in a directory that corresponds to the package name of the class.
....\ vehicle \ Car . java
The correct class name and path will now look like this:
  • Class Name -> vehicle.Car
  • Path Name -> vehicle\Car.java (in windows system)
Usually, a company uses its reversed form of the Internet domain name as its package name. For example, the Internet domain name is runoob.com, and all package names begin with com.interviewbubbleEach section in the package name corresponds to a subdirectory.
For example: There is a package for com.interviewbubble.test . This package contains a source file called Runoob.java. Correspondingly, there should be a series of subdirectories like this:
....\ com \ interviewbubble \ test \ bubble . 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, plus .class as the extension suffix. E.g:
// File Name: Runoob.java package com . interviewbubble . test ; public class bubble {

}
class Google {

}
Now, we compile this file with the -d option, as follows:
$javac - d . bubble . java 
This places the compiled file like this:
. \com\ interviewbubble\test\ bubble . class . \com\ interviewbubble\test\Google . class
You can import all the classes, interfaces, etc defined in \com\runoob\test\ as follows:
import com . interviewbubble . test .*;
The compiled .class files should be identical to the .java source files, and the directory they are placed in should correspond to the package name. However, there is no requirement that the path to the .class file be the same as the corresponding .java path. You can arrange source and class directories separately.
<path-one> \sources\com\ interviewbubble\test\bubble.java<path-two> \classes\com\ interviewbubble\test\bubble.class
In this way, you can share your class catalog with other programmers without revealing your source code. Managing source code and class files in this way allows the compiler and the Java virtual machine (JVM) to find all the 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 the 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.runoob.test, and the compiler and JVM find the .class file in <path-two>\classes\com\interviewbubble\test.
A class path may contain several paths. Multipaths should be separated by delimiters. By default, the compiler and JVM find the current directory. JAR files contain classes related to the Java platform, so their directories are by default placed in the class path.

Set the 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 the Bourne shell): # echo $CLASSPATH
Delete the contents of the current CLASSPATH variable:
  • Windows platform (under DOS command line): C:\> set CLASSPATH=
  • UNIX platform (under the Bourne shell): # unset CLASSPATH; export CLASSPATH
Set the CLASSPATH variable:
  • Windows platform (under DOS command line): C:\> set CLASSPATH=C:\users\jack\java\classes
  • UNIX platform (under the Bourne shell): # CLASSPATH=/home/jack/java/classes; export CLASSPATH

Comments