Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java Package


May 10, 2021 Java


Table of contents


Java Package

To better organize classes, Java provides a package mechanism for distinguishing namespaces for class names.

The role of the package

  • 1 Organize functionally similar or related classes or interfaces in the same package for easy classes to find and use.
  • 2 Like folders, packages are stored in tree directories. T he class names in the same package are different, the names of the classes in different packages can be the same, and when classes with the same class name in two different packages are called at the same time, the package names should be added to distinguish them. T herefore, the package avoids name conflicts.
  • 3 Packages also restrict access to classes that have package access to access classes in a package.

Java uses packages as a mechanism to prevent naming conflicts, access control, provide search and location classes, interfaces, enumerations, annotations, and so on.

The syntax format of the package statement is:

package pkg1[.pkg2[.pkg3…]];

For example, a Samething .java contents of a file

package net.java.util
public class Something{
   ...
}

Then its path should be net/java/util/something.java saved like this. The role of packages is to save different java program classifications and make it easier to be called by other java programs.

A package can be defined as a set of interrelated types (classes, interfaces, enumerations, and comments) that provide access protection and namespace management capabilities for those types.

Here are some of the packages in Java:

  • java.lang - package the underlying class
  • java.io - A function that contains input and output functions

Developers can package a set of classes and interfaces themselves and define their own packages. A nd it's worth advocating in real-world development, where you group related classes when you've done the class implementation yourself, making it easier for other programmers to determine which classes, interfaces, enumerations, comments, and so on are related.

Because package creates a new namespace, there is no naming conflict with any other name in the package. U sing the package mechanism makes access control easier and makes it easier to locate related classes.


Create a package

When you create a package, you need to give it a proper name. A fter that, if another source file contains the class, interface, enumeration, or comment type provided by the package, you must place the stack declaration at the beginning of the source file.

Package declarations should be on the first line of the source file, with only one package declaration per source file, and each type in the file applies to it.

If a package declaration is not used in a source file, the classes, functions, enumerations, comments, and so on are placed in an u-named package.

Example

Let's look at an example that created a package called animals. L owercase letters are often used to avoid conflicts with class and interface names.

Add an interface (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();
   }
} 

The two files are then compiled and placed in a subdirecte called animals. Run with the following command:

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

Import keywords

In order to be able to use the members of a package, we need to explicitly import the package in the Java program. U se the "import" statement to accomplish this feature.

The import statement in the java source file should be after the package statement, before all classes are defined, there can be no, or there can be more than one, in the syntax format:

import package1[.package2…].(classname|*);

If one class wants to use another class in one package, the package name can be omitted.

Example

The following payroll package already contains the Employee class, and then add a Boss class to the payroll package. T he Boss class can refer to the Emloyee class without using the payroll prefix, as shown in the case of the Boss class.

package payroll;

public class Boss
{
   public void payEmployee(Employee e)
   {
      e.mailCheck();
   }
}

What if the Boss class isn't in the payroll package? B oss classes must refer to classes in other packages using one of several methods

Use the class full name 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;

Attention:

The class file can contain any number of impport declarations. T he import declaration must be after the package declaration, before the class declaration.


The directory structure of the package

There are two main results when a class is placed in a package:

  • Package names become part of class names, as we discussed earlier.
  • The package name must match the directory structure in which the corresponding bytecode is located.

Here's an Chinese manage your own javaware:

Put the source code for classes, interfaces, and so on in a file whose name is the name of the type, .java 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 in which the class is located.

....\vehicle\Car.java

Now, the correct class name and path will look like this:

  • Class name - sgt; vehicle. Car

  • Path Name --vehicle-car.java (in windows)

Usually, a company uses the inverted form of its Internet domain name as its package name. F or example, internet domain names apple.com, and all package names start with com.apple. E ach part of the package name corresponds to a subdirecte.

For example, the company has a com.apple.computers package that contains a source file called Dell.java, which should be followed by a series of subdirectts:

....\com\apple\computers\Dell.java

At compile time, the compiler creates a different output file for each class, interface, and other types defined in the package, the name of which is the name of the type, with .class as the extension suffix. For example:

// 文件名: Dell.java

package com.apple.computers;
public class Dell{
      
}
class Ups{
      
}

Now let's compile this file with the -d option, as follows:

$javac -d . Dell.java

This places the compiled file like this:

.\com\apple\computers\Dell.class.\com\apple\computers\Ups.class

You can import all the classes, interfaces, and so on defined in the .

import com.apple.computers.*;

The compiled .class files should be .java the original source files, and the directories they place should correspond to the name of the package. H owever, it is not .class the path of the file to .java path of the corresponding file. Y ou can arrange the directory of source code and classes separately.

<path-one>\sources\com\apple\computers\Dell.java
<path-two>\classes\com\apple\computers\Dell.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 the types used in your program.

The absolute path to the class directory is called class path. S et in the system variable CLASSPATH. The compiler and java virtual machines later constructed the path to the file by .class the class path.

The name of the package is com.apple.computers, and the compiler and JV.class M will look for the file in the name of the class path.

A class path may contain several paths. M ultipaths should be separated by separators. B y default, the compiler and JVM look up the current directory. JAR files are based on classes that contain Java platform-related, so their directories are placed in class path by default.


Set the CLASSPATH system variable

Show the current CLASSPATH variable with the following command:

  • Windows Platform (down the DOS command line) - C: set CLASSPATH
  • UNIX Platform (under Bourne shell) - - $CLASSPATH -
Remove the contents of the current CLASSPATH variable:
  • Windows Platform (down the DOS command line) - C: set CLASSPATH
  • UNIX Platform (under Bourne shell) - - export CLASSPATH

Set the CLASSPATH variable:

  • Windows Platform (down the DOS command line) - set CLASSPATH
  • UNIX Platform (under Bourne shell) - - CLASSPATH - / home / jack / java / classes; export CLASSPATH