Java Package

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 or sub-package. Using package it becomes easier to locate the related classes and it also provides a good structure for projects with hundreds of classes and other files.


Types of Packages: Built-in and User defined

  • Built-in Package: Existing Java package for example java.lang, java.util etc.
  • User-defined-package: Java package created by user to categorize their project's classes and interface.

Packages in java


Creating a package

Creating a package in java is quite easy. Simply include a package command followed by name of the package as the first statement in java source file.

package mypack;
public class employee 
{
    statement; 
}

The above statement will create a package woth name mypack in the project directory.

Java uses file system directories to store packages. For example the .java file for any class you define to be part of mypack package must be stored in a directory called mypack.

Additional points about package:

  • A package is always defined as a separate folder having the same name as the package name.
  • Store all the classes in that package folder.
  • All classes of the package which we wish to access outside the package must be declared public.
  • All classes within the package must have the package statement as its first line.
  • All classes of the package must be compiled before use (So that they are error free)

Example of Java packages

//save as FirstProgram.java  
package learnjava;  
public class FirstProgram{  
    public static void main(String args[]) {  
        System.out.println("Welcome to package");  
    }  
}

How to compile Java programs inside packages?

This is just like compiling a normal java program. If you are not using any IDE, you need to follow the steps given below to successfully compile your packages:

javac -d directory javafilename

Example:

javac -d . FirstProgram.java

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


How to run Java package program?

You need to use fully qualified name e.g. learnjava.FirstProgram etc to run the class.

To Compile:

javac -d . FirstProgram.java

To Run:

java learnjava.FirstProgram

Output: Welcome to package


import keyword

import keyword is used to import built-in and user-defined packages into your java source file so that your class can refer to a class that is in another package by directly using its name.

There are 3 different ways to refer to any class that is present in a different package:

  1. Using fully qualified name (But this is not a good practice.)

    If you use fully qualified name to import any class into your program, then only that particular class of the package will be accessible in your program, other classes in the same package will not be accessible. For this approach, there is no need to use the import statement. But you will have to use the fully qualified name every time you are accessing the class or the interface, which can look a little untidy if the package name is long.

    This is generally used when two packages have classes with same names. For example: 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();  
        }  
    }

    Output:

    Hello

  2. To import only the class/classes you want to use

    If you import packagename.classname then only the class with name classname in the package with name packagename will be available for use.

    Example :

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

    Output:

    Hello

  3. To import all the classes from a particular package

    If you use packagename.*, then all the classes and interfaces of this package will be accessible but the classes and interface inside the subpackages will not be available for use.

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

    Example :

    //save by First.java  
    package learnjava;  
    public class First{  
        public void msg() {
            System.out.println("Hello");
        }  
    }  
    
    //save by Second.java  
    package Java;  
    import learnjava.*;    
    class Second {  
        public static void main(String args[]) {  
            First obj = new First();  
            obj.msg();  
        }  
    }

    Output:

    Hello


Points to remember

  • When a package name is not specified, the classes are defined into the default package (the current working directory) and the package itself is given no name. That is why, you were able to execute assignments earlier.
  • While creating a package, care should be taken that the statement for creating package must be written before any other import statements.
// not allowed
import package p1.*;
package p3;

Below code is correct, while the code mentioned above is incorrect.

//correct syntax
package p3;
import package p1.*;