Package in Java

 

A group of classes, interfaces and sub packages of similar type is known as a Java package. There are two forms of packages in Java:

  1. Built in package
  2. User defined package

The built in packages of java include lang, awt, javax, sql, etc.

 

Advantage of Java Package:

We use java packages to maintain classes and interfaces by categorizing them. Java packages are also used to provide access protection and to remove naming collision.

 

Simple example of Java package:

A package in Java can be created by using the keyword “package”. Consider the following example:

package Example;

public class A {

public static void main (string args []) {

system. out. println (“Using Packages”);

}

}

 

How to compile Java package?

A java package can be compiled by using the following syntax:

javac –d directory javafilename

Example:

javac –d Example. java

The –d is a switch that tells the destination to put the file of generated class. We can use any name of directory to put this file and if the user wants to save the file in the current or same directory then (.) dot can be used.

 

How to run Java package program?

The java package program can be run using the fully qualified name:

To compile: javac –d . Example. java

To run: java PackageExample. Example

 

How to access package from another package?

In Java there are three different ways to run a package from another package:

  1. import package.*;
  2. import package. name of class
  3. fully qualified name

 

Using packagename. *:

By using “packagename.*” we will be able to access all the classes and interfaces of the package. But we cannot access the sub packages.

We use the import keyword to access the classes and interfaces of other package to current package.

 

Example of package that imports the packagename.*:

Consider the following example in which package is imported:

CODE:

package A;

public class B {

public void display () {

system. out. println (“Hello World”);

}

}

package S;

import A.*;

class C {

public static void main (string args []) {

B b1 = new B ();

b1. display ();

}

}

OUTPUT:

Hello World

 

Using packagename. classname:

If we use package. classname then only the declared class of the package will be available.

 

Example of package by import package. classname:

Consider the following example:

CODE:

package A;

public class B {

public void display () {

system. out. println (“Hello World”);

}

}

package S;

import A. B;

class C {

public static void main (string args []) {

B b1 = new B ();

b1. display ();

}

}

OUTPUT:

Hello World

 

Using fully qualified name:

Only the declared class of the package will be accessible if the fully qualified name is used. There is no need of the import keyword when using fully qualified name. But the fully qualified name should be used each time when accessing the class or interface.

 

Example of package by import fully qualified name:

Consider the following example in which fully qualified name is used:

CODE:

package A;

public class B {

public void display () {

system. out. println (“Hello World”);

}

}

package S;

class C {

public static void main (string args []) {

A.B = new A.B ();

b1. display ();

}

}

OUTPUT:

Hello World

When a package is imported the sub packages of this will not be imported. When a package is imported all classes and interfaces of that package are imported except the classes and interfaces of sub packages. Therefore, we need to import sub packages also.

 

Sub package in Java:

A package inside another package is called sub package.

 

Example of sub package:

Consider the following example:

CODE:

package com. javat. core;

class Example {

public static void main (string args []) {

system. out. println (“Hello World”);

}

}

To compile: javac –d Example. java

To run: java com. javat. core. Example

OUTPUT:

Hello World

 

How to send class file to another directory or drive?

Consider the following code to send class file to another directory:

CODE:

package A;

public class Example {

public static void main (string args []) {

system. out. println (“Hello world”);

}

}

 

To compile:

e:\sources> javac -d c:\classes Example.java

 

To run:

The program can be run from e:\ source directory by setting class path.

 

Ways to load class files or jar files:

In java there are two ways to load files:

  1. Temporary
  2. Permanent

We can load the file temporary by setting the class path in the command prompt and can load the file permanently by setting the class path in environment variables.

It should be noted that a java source file can contain only one public class which should be saved by the name of public class.

 

How to put two classes in a package?

Two classes can be put into java package by creating two source files which has one public class but the name of the package should be the same.