Java - Package

A Java package is a mechanism for organizing Java classes. Package are used in Java, in-order to avoid name conflicts and to control access of class, interface and enumeration etc. A package can be defined as a group of similar types of classes, interface, enumeration and sub-packages. We can define package using package keyword.

 

Built-in Package :- 

Existing built-in java packages like java.lang, java.util, java.sql etc.

 

User-define package :-

Java package created by user to categorized classes, interface and enumeration.

 

Creating a Package :-

 

Creating a package in java is quite easy. Simply include a package command follow by name of package as a first statement of java source file. There can be only one package statement in each source file.

 

If package statement not used in source file than it takes default package for all classes, interfaces and enumeration.

 

Example:


package firstpack;
public class Test{
    public static void main(String args[]){
      System.out.println(“Testing package”);
    }
}

 

To run this program:

 

- Create firstpack folder in your currently development working directory.

- Compile the source file.

- Put the class file into the directory you have created.

- Execute the program from development directory.

 

 

Package is a way to organize files in Java, it is used when project consist multiple modules. Package’s access level also allows you to protect data from being used by the non-authorized classes.

 

 

import keyword:

 

import keyword is used to import built-in and user defined packages into your java source files.  If a class wants to use another class in the same package, the package name does not need to be used. Classes in to the same package use without any import statement.

 

Import statement is kept after package statement.

package firstpackage;

import  java.util.Date;

 

There are three ways to refer that is present in different package.

1) Using fully qualified name

Example :


class Test extends java.util.Date{

 <<statements>>
}

 

2) import the class you want to use :

Example:


import java.util.Date;

class Test extends Date{
  <<statements>>
}

 

3) import all the classes from the particular package:

Example:


import java.util.*;

class Test extends Date{
  <<statements>>
}

 

 

Static import:

Static Import is a new feature added in Java 5 specification

In order to access static members, it is necessary to qualify references with the class they came from. For example, one must say:

 

double r = Math.cos(Math.PI * theta);

or

System.out.println("Testing");

 

You may want to avoid unnecessary use of static class members like Math. and System. For this use static import. For example above code when changed using static import is changed to:

 


import static java.lang.System.out;
import static java.lang.Math.PI;
import static java.lang.Math.cos;
class Test{
public static void main(String args[]){
  double r = cos(PI * theta);
  out.println("Testing");

}
}

 

So whats the advantage of using above technique? Only advantage that I see is readability of the code. Instead of writing name of static class, one can directly write the method or member variable name.

 

Also keep one thing in mind here. Ambiguous static import is not allowed. i.e. if you have imported java.lang.Math.PI and you want to import mypackage.Someclass.PI, the compiler will throw an error. Thus you can import only one member PI.