Java Package

Packages in java are those class which contains methods without the main method in them.

  • Packages are mainly used to reuse the classes which are already created/used in other program
  • We can define many number of classes in the same package
  • Packages are mainly divided into two types
    • Built in packages
    • User defined Packages

The main use of packages in java

  • Java package is used to categorize the classes and interfaces so that they can be easily maintained.
  • Java package provides access protection
  • Java package removes naming collision

Frequently used built in packages are:

  • lang
  • util
  • io
  • awt
  • net
  • applet
  • *These built in packages are placed in the package “java”.
  • In order to use these packages/classes present in the package we need to import them in the current program
  • The syntax for importing packages is: import java.util.*
  • The asterisk symbol tells the compiler to import all the classes in present in the util package.If you want to import specific class then we need to mention the name of the class instead of the asterisk.For eg: import java.util.Scanner

java.lang:

Language supports classes.These are classes that java compiler itself uses and therefore automatically imported.They include classes for primitive types, stirngs, maths, threads and exceptions.

java.util:

Language utility classes such as vectors, hash tables, random numbers, date etc

java.io:

input/output support classes.They provide the facility for input/output of data.

java.awt:

Set of classes for implementing graphical user interface.They include classes for windows, buttons, lists

java.net:

Classes for networking. They include classes for communicating with local computers as well as with internal servers

Create a user defined package

For this we need to create a folder with your desired name say mypackage

package mypackage; 
public class A
{
 public void display()
 {
 System.out.println("Hello world");
 }
}

How to user define package in other programme

Example

import mypackage.*;
class packagedemo
{
 public static void main(String arg[])
 {
 A ob = new A();
 ob.display();
 }
}

Steps for creating package

To create a user defined package the following steps should be involved

  • Declare the package at the beginning of a file using the syntax :-
    • package packageName;
  • Define the class that is to be put in the package & declare it public.
  • Create a subdirectory under the directory where the main source files are stored.
  • Store the listing as the classname. java file in the subdirectory created
  • Compile the file. This create .class file in the subdirectory.
  • Compile the file. This create .class file in the subdirectory
package mypack; 
public class Simple{ 
public static void main(String args[]){ 
 System.out.println("Welcome to package"); 
 } 
}
  • If you are not using any IDE, you need to follow the syntax given below
    • javac -d directory javafilename

For example

javac -d . Simple.java

The -d switch specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use . (dot).

How to compile java package

we need to use fully qualified name e.g. mypack.Simple etc to run the class.

To Compile: javac -d . Simple.java

To Run: java mypack.Simple

Output:Welcome to package

The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The . represents the current folder

Java also supports the concept of package hierarchy. This is done by specifying multiple names in a package statement, seprated by dots (.)

Ex :- package firstPackage.secondPackage;

This approach allows us to group related classes into a package and their group related package into a larger package. Store this package in a subdirectory named firstpackage/secondPackage.

A java package file can have more than one class definition. In such cases, only one of the classes may be declared public & that class name with .java extension is the source file name. When a source file with more than one class definition is compiled, java creates independent .class files for those classes.

How to Access a package

There are three ways to access the package from outside the package

  • import package.*;
  • import package.classname;
  • fully qualified name.

If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages

The import keyword is used to make the classes and interface of another package accessible to the current package

Using package name

Example

//save by A.java 
package pack; 
public class A{ 
 public void msg(){System.out.println("Hello");} 
} 
//save by B.java
package mypack;

class B{ 
 public static void main(String args[]){ 
 A obj = new A(); 
 obj.msg(); 
 } 
}
Output:Hello

Using packagename.classname

If you import package.classname then only declared class of this package will be accessible.

Example

import pack.A; 
class B{ 
 public static void main(String args[]){ 
 A obj = new A(); 
 obj.msg(); 
 } 
} 

Using fully qualified name

If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface.

It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class

Example

//save by A.java 
package pack; 
public class A{ 
 public void msg(){System.out.println("Hello");} 
} 
//save by B.java 
package mypack; 
class B{ 
 public static void main(String args[]){ 
 pack.A obj = new pack.A();//using fully qualified name 
 obj.msg(); 
 }
}

Leave a Comment