Latest web development tutorials

Java package (package)

In order to better organize the class, Java provides a package mechanism for the difference between the class name of the namespace.

The role of the package

  • A function similar or related organization of the class or interface in the same package, to find and easy to use class.
  • 2 as the same folder, the package also used the storage directory tree. Classes in the same package name is different, a different package name of the class is the same, while simultaneously calling two different types of packages in the same class name, package name should be added distinction. Thus, the package name to avoid conflict.
  • 3 package also defines the access rights, the classes have access to the package to access a package of classes.

Java uses package (package) This mechanism is designed to prevent naming conflicts, access control, providing search and locate class (class), interfaces, enumerations (enumerations) and comments (annotation) and the like.

Package statement syntax is:

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

For example, a file its content Something.java

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

Then it should be the path net / java / util / Something.java so saved. package (package) role is to classify different java program stored more easily be called by other java program.

A package (package) can be defined as a set of types (classes, interfaces, enumerations, and annotation) interconnected to provide access protection and namespace management functions for these types.

Here are some of Java in the package:

  • java.lang- packaged base class
  • Java.io- function contains input and output functions

Developers can put their own set of classes and interfaces, packaged, and define your own package. And in the actual development is to be encouraged to do so, when you complete the implementation class, grouping related classes, so that other programmers can more easily determine which classes, interfaces, enumerations, and comments are related .

Since the package creates a new namespace (namespace), so without naming conflicts with any other package name. Use this package mechanism, easier to implement access control, and let locate the relevant class easier.


Create package

Create a package, you need to take an appropriate name for this package. Then, if a source file that contains the other classes offered this package, interface, annotation or enum type, the package this declaration must be placed at the beginning of this source file.

Package should be declared in the first line of the source file for each source file can have only one package statement, the file for each type are applied to it.

If you do not use a package statement in the source file, then one of the classes, functions, enumeration, comments, etc. will be placed in an unnamed package (unnamed package) in.

example

Let's look at an example that creates a package called the animals. Generally use lowercase letters to avoid naming classes, interfaces, and the name of the conflict.

Join a package interface animals (interface):

/* 文件名: Animal.java */
package animals;

interface Animal {
   public void eat();
   public void travel();
}

Next, add in the same package that implements the interface:

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 put them in a subdirectory named animals. Use the following command to run:

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

import keywords

To be able to use a member of a package, we need to explicitly import the package in the Java program. Using the "import" statement to perform this function.

In java source file import statements should be located after the package statement, all previously defined classes, you can not, you can also have multiple, its syntax is:

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

If a package, a class that you want to use this package in another class, then the package name can be omitted.

example

The following payroll package already contains the Employee class, then add a class to the Boss payroll package. When Boss Employee class reference class can not use payroll prefix instances Boss class follows.

package payroll;

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

What if the class is not Boss payroll package will? Boss class must use one of the following methods to reference other classes in the package

Use the full name of the class description, such as:

payroll.Employee

With the introduction of import keywords, use the wildcard "*"

import payroll.*;

Using import keyword introduces Employee Class

import payroll.Employee;

note:

Class file can contain any number of import declaration. import declaration must be after the package statement, before the class declaration.


package directory structure

Class on the package have two main results:

  • Package name becomes part of the class name, as we discussed earlier.
  • Package name must match the corresponding byte code in the same directory structure match.

Here is a simple way to manage your files in java:

The classes, interfaces, and other types of source code in a text, the name of this file is the name of the type, and .java extension. E.g:

// 文件名 :  Car.java

package vehicle;

public class Car {
   // 类实现  
}

Next, the source files in a directory, the directory name to the corresponding class of its own package.

....\vehicle\Car.java

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

  • Class name -> vehicle.Car

  • Pathname -> vehicle \ Car.java (in windows)

Typically, a company uses its reversed form of Internet domain names as its package name, for example: the Internet domain is apple.com, all the package names are in com.apple beginning. Each package name corresponding to a part of a subdirectory.

For example: The company has a com.apple.computers the package, which contains a known Dell.java source file, the corresponding should be like the following series of subdirectories:

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

Compile time, the compiler creates a different output file, the output file name is the name of this type is defined in the package type of each class, interface, and added as an extension .class suffix. E.g:

// 文件名: Dell.java

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

Now, we use the -d option to compile the file, as follows:

$javac -d . Dell.java

This will place like this to compile the file:

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

You can like this to import all \ com \ apple \ computers \ class defined in the interface:

import com.apple.computers.*;

.class files compiled after and should .java source files, they are placed in the directory should correspond with the name of the package. However, the path does not require .class file with path corresponding .java same. You can arrange separate source and class directories.

<path-one>\sources\com\apple\computers\Dell.java
<path-two>\classes\com\apple\computers\Dell.class

In this way, you can share your classes directory to other programmers, rather than reveal their source. In this way manage source and class files can let the compiler and java virtual machine (JVM) you can find all types used in the program.

Absolute directory paths class called class path. Set in the system variable CLASSPATH. Compiler and the java virtual machine by the package name to the class path later path construction .class file.

<Path- two> \ classes is the class path, package name is com.apple.computers, and the compiler and JVM will be in <path-two> \ classes \ com \ apple \ compters find the .class files.

A class path might contain several paths. Multi-path should be separated by a delimiter. By default, the compiler and JVM find the current directory. JAR file containing the Java platform, according to the relevant class, so they default on a directory in the class path.


Setting CLASSPATH system variable

Use the following command to display the current CLASSPATH variable:

  • Windows platforms (DOS command line) -> C: \> set CLASSPATH
  • UNIX platforms (Bourne shell under) ->% echo $ CLASSPATH
Remove the current CLASSPATH variable content:
  • Windows platforms (DOS command line) -> C: \> set CLASSPATH =
  • UNIX platforms (Bourne shell under) ->% unset CLASSPATH; export CLASSPATH

Setting CLASSPATH variable:

  • Windows platforms (DOS command line) -> set CLASSPATH = C: \ users \ jack \ java \ classes
  • UNIX platforms (Bourne shell under) ->% CLASSPATH = / home / jack / java / classes; export CLASSPATH