Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java package

Java package

To better organize classes, Java provides a package mechanism used to distinguish the namespace of class names.

The role of packages

Java uses the package mechanism to prevent naming conflicts, control access, and provide searching and locating of classes, interfaces, enumerations, and annotations.

The syntax format of the package statement is:

package

pkg1

[

.

pkg2

[

.

pkg3

...

]

]

;

For example, a Something.java file with its content

package

net

.

java

.

util

;

public

class

Something

{

...

}

Then its path should be net/java/util/Something.java saved this way. The role of package is to save different Java programs in categories, making it more convenient for other Java programs to call them.

A package can be defined as a group of interrelated types (classes, interfaces, enumerations, and annotations), providing access protection and namespace management functions for these types.

The following are some packages in Java:

Developers can package a group of classes and interfaces themselves and define their own packages. Moreover, doing so is encouraged in actual development; after you complete the implementation of classes, grouping related classes makes it easier for other programmers to determine which classes, interfaces, enumerations, and annotations are related.

Since packages create a new namespace, they will not cause naming conflicts with any names in other packages. Using the package mechanism makes access control easier to implement and makes locating related classes simpler.


Creating a package

When creating a package, you need to give it an appropriate name. After that, if another source file contains class, interface, enumeration, 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. Each source file can have only one package declaration, and each type in the file applies to it.

If a source file does not use a package declaration, then its classes, functions, enumerations, annotations, etc. will be placed in an unnamed package.

Example

Let us look at an example that creates a package called animals. Lowercase letters are usually used for naming to avoid conflicts with class 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 implementation of this interface to 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 these two files and place them in a subdirectory called animals. Run them with the following command:


$ mkdir animals
$ cp Animal.class  MammalInt.class animals
$ java animals/MammalInt
Mammal eats
Mammal travel

import keyword

In order to use a member of a package, we need to explicitly import that package in the Java program.

In Java, the import keyword is used to import types defined in other classes or packages, so that these types can be used in the current source file.

The import keyword is used to introduce classes, interfaces, or static members from other packages; it allows you to use classes from other packages directly in your code without fully specifying the class's package name.

In a Java source file, the import statement must be located at the head of the Java source file, and its syntax format is:

import

package1

[

.

package2

...

]

.

(

classname

|*

)

;

The import statement is placed after the package statement:

//

The first non-comment line is the package statement

package

com

.

example

;

//

The import statement introduces classes from other packages

import

java

.

util

.

ArrayList

;

import

java

.

util

.

List

;

//

Definition of the class

public

class

MyClass

{

//

Members and methods of the class

}

If, in a package, a class wants to use another class in the same package, then the package name can be omitted.

You can use an import statement to import a specific class:


import com.runoob.MyClass;

This way, you can directly use the methods, variables, or constants of the MyClass class in the current source file.

You can also use the wildcard * to import an entire package or subpackage:


import com.runoob.mypackage.*;

This way, you can import com.runoob.mypackage all classes in the package, and thus use the methods, variables, or constants of any class in the package in the current source file. Note that when using the wildcard * to import an entire package, only the classes in the package are imported, not the subpackages.

When importing a class or package, you need to provide the fully qualified name of the class or package. The fully qualified name consists of the combination of the package name and the class name, separated by a dot.

Example

import

java.util.ArrayList

;

// Import the ArrayList class from the java.util package

import

java.util.*

;

// Import all classes from the java.util package

import

com.example.MyClass

;

// Import the MyClass class from the com.example package

import

com.example.*

;

// Import all classes from the com.example package

Example

The payroll package below already contains the Employee class. Next, add a Boss class to the payroll package. When the Boss class references the Employee class, it does not need to use the payroll prefix. The Boss class instance is 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 fully qualified class name, for example:

payroll

.

Employee

Using import Introduce with the keyword, using the wildcard *:

import

payroll

.*;

Use import Introduce the Employee class with the keyword:

import

payroll

.

Employee

;

**Note:**

A class file can contain any number of import declarations. Import declarations must be after the package declaration and before the class declaration.


The directory structure of a package

Placing a class in a package has two main results:

The following is a simple way to manage your own Java files:

Place the source code of types such as classes and interfaces in a text file whose name is the name of the type, with .java as the extension. For example:

//

File name: Car.java

package

vehicle

;

public

class

Car

{

//

Class implementation

}

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:

Typically, a company uses the reverse form of its Internet domain name as its package name. For example: if the Internet domain name is runoob.com, all package names start with com.runoob. Each part of the package name corresponds to a subdirectory.

For example: there is a com.runoob.test package that contains a source file called Runoob.java; correspondingly, there should be the following series of subdirectories:

....\

com

\

runoob

\

test

\

Runoob

.

java

At compile time, the compiler creates a separate output file for each type such as class or interface defined in the package. The output file name is the name of the type, with .class appended as the extension. For example:

//

File name: Runoob.java

package

com

.

runoob

.

test

;

public

class

Runoob

{

}

class

Google

{

}

Now, we compile this file with the -d option, as follows:


$javac -d . Runoob.java

This will place the compiled files as follows:


.\com\runoob\test\Runoob.class
.\com\runoob\test\Google.class

You can import all classes, interfaces, etc. defined in **\com\runoob\test\** as follows:


import com.runoob.test.*;

The compiled .class files should, like the .java source files, be placed in directories corresponding to the package names. However, it is not required that the path of the .class file be the same as that of the corresponding .java file. You can arrange the source and class directories separately.


\sources\com\runoob\test\Runoob.java
\classes\com\runoob\test\Google.class

This way, you can share your class directory with other programmers without revealing your source code. Managing source and class files this way allows the compiler and the Java Virtual Machine (JVM) to find all the types used in your program.

The absolute path of the class directory is called class pathand is set in the system variable CLASSPATH . The compiler and the JVM construct the path of the .class file by appending the package name to the class path.

\classes is the class path, the package name is com.runoob.test, and the compiler and JVM will look in \classes\com\runoob\test for the .class files.

A class path may contain several paths, and multiple paths should be separated by a separator. By default, the compiler and JVM look in the current directory. JAR files hold Java platform-related classes, so their directories are placed in the class path by default.


Setting the CLASSPATH system variable

Display the current CLASSPATH variable with the following command:

Delete the current CLASSPATH variable content:

Set the CLASSPATH variable: