Java: package and import Keyword

Java package is a mechanism to a group of similar types of classes, interfaces, and sub-packages. java Package categorizes in two forms:

  1. Built-in Package
  2. User Define Package

Built-in Package

Java provides lots of built-in packages as per specific requirements. Here is a list of most frequently used built-in packages:

Package Name Description
java.applet It provides the classes related to applet creation and communicates with its applet context.
java.awt It contains the classes for creating user interfaces designing graphics and images.
java.beans It provides classes related to developing components based on the JavaBeans architecture. Provides classes and interfaces for input and output through data streams, serialization, and the file system.
java.lang It provides fundamental classes to the design of the Java programming language.
java.math It provides classes for performing mathematical operations for integer arithmetic (BigInteger) and decimal arithmetic (BigDecimal). Provides the classes related to implementing networking applications.
java.nio Defines buffers classes which are containers for data.
java.rmi Provides the classes related to RMI. Provides the interfaces and classes for the security framework.
java.sql Provides the API for processing and accessing data stored in a data source.
java.time The Classes and Methods to deal with dates, times, instants, and durations.
java.util Provide Collections framework classes and interfaces, date and time facilities, internationalization, and miscellaneous utility classes like (string tokenizer, random-number generator, and bit array).

You can get refer the complete list of Java packages from this link : Java: Complete Package List

java Most Common Built in packages

Here we will mainly focus on User Defined Packages creation and access.

Advantage of Java Package

  1. Java package is used to group the classes and interfaces so that they can be easily maintained.
  2. Java package removes naming conflicts.
  3. Java package provides access protection.

Rule of Java Package

There are certain rules that need to follow while creating the java class files.

  • Rule 1: Declare package as the first line of the statement in class or interface or a .java file. Which shows the directory location of java file after compilation.
  • Rule 2: A single java source file can have only one public class and this file will save with public class name only.
  • Rule 3: Two same name source files can not be in the same package.
  • Rule 4: If a source file is not having a package name i.e consider in the default package.
  • Rule 5: A class accessing another class, if both are in the same package then don’t require a package name.

Java Package Example

package mypackage;
public class SimplePackageExample{
public static void main(String args[]){
System.out.println("Welcome to My Package");

How Java Package Compilation?

If you are not using any IDE, you need to follow the syntax in command-line as given below:

javac -d directory java_file_name  

For example:

javac -d .  

The -d argument represents destination i.e use switch specifies the destination where to put the generated class file.

Note: Use dot(.) to keep package within the same directory/current folder. You can put directory path in place of the dot(.) to move compiled class file on that location. For Example Use /home (in case of Linux), d:/myclasses (in case of windows), etc.

Java Package Program Execution

You need to use a fully qualified name e.g. mypackage.SimplePackageExample etc to run the class.

To Compile: javac -d .
To Run: java mypackage.SimplePackageExample
Output:Welcome to My Package

How to access package classes from another package?

Java provides three ways to access the package from outside the package:

  1. import package.*;
  2. import package.classname;
  3. fully qualified name.

import keyword

The import keyword is used to access classes and the interface of another package.

Suppose we have ClassA inside package mypackageA. Now you will see the access to this class in another package in a different way in a further post.

package mypackageA;
public class ClassA{
  public void getMessage(){
  System.out.println("Hello In Package A");

Using import packagename.*

If you use an import package.* then all the classes and interfaces of this package will be accessible but not sub-packages.

Here in this example using ClassA from another package that’s what use import mypackageA.* to import all classes and interfaces on this package.

package mypackageB;
import mypackageA.*
public class ClassB{
public static void main(String[]args)
   ClassA obj = new ClassA();

Note: This way of import definitely reduces the line of import statement but tries to avoid using it because if the declared package in having lots of classes and interfaces then it will load all. Definitely, it will impact your performance.

Using import packagename.className

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

In this below case for statement package mypackageA.ClassA only classA in package mypackageA will load.

package mypackageB;
import mypackageA.ClassA
public class ClassB{
public static void main(String[]args)
   ClassA obj = new ClassA();

Using the fully qualified name

If you are using the fully qualified name of class or interface on time of declaration of reference object then only declared class of this package will be accessible. Now no need to import statements.
But the problem is you need to use a fully qualified name every time when you are accessing the class or interface.

Note: This way generally preferred when two packages have the same class name for example Date Class is in java.sql and java.util package.

package mypackageB;
public class ClassB{
public static void main(String[]args)
   mypackageA.ClassA obj = new mypackageA.ClassA();
   //another instance need to give full qualified name again.
   mypackageA.ClassA obj1 = new mypackageA.ClassA();

Now you have learned about package creation, access to another class, compilation and execution of classes. Here you will learn about subpackage and way of creation it.

Java SubPackage

The package within the package is called the subpackage. It should be created to further categorize the package.

Note: If you import a package all the classes and interface of that package excluding subpackage. Hence need to import sub package separately if required.

The standard way of defining a package is for example:

//admin related packages

//user related packages

In the above packages you can easily understand the code layer and modules and when you need to understand the code you can easily understand it.

As mentioned above builtin packages having related classes with the packages and sub-packages. For Example, Input/Output related classes in package, Server and ServerSocket classes in packages and SQL related classes in java.sql packages.

Example of java Subpackage

package com.fiot.subpackage.example;
public class SimpleSubPackageExample{
 public static void main(String args[]){
    System.out.println("Welcome to My Sub Package");
To Compile: javac -d .
To Run: java com.fiot.subpackage.example.SimpleSubPackageExample
Output:Welcome to My Sub Package

When you compile above program it will create SimpleSubPackageExample.class file inside the folder (current_path/com/fiot/subpackage.example). When running this file you have to mention complete package with the file name as given in the above example.

Move the class file to another drive or directory

Let consider for the above example and want to send this file classes folder in e: drive when compile. We have run as below:

To Compile: current_file_directory> javac -d e:\classes

To run:
To run the program from the current directory, you need to set the classpath of the directory where the class files reside.

current_directory> set classpath=e:\classes;.;
current_directory> java -classpath com.fiot.subpackage.example.SimpleSubPackageExample

Note: The -classpath switch can be used with javac and java tools.

Ways to load the jar or class files

Java supports two ways to load the class files:

  1. Temporary
    By setting the classpath in the command prompt as mentioned in the above example.
By -classpath switch

By setting the classpath in the environment variables

Create the jar file, that contains all the class files and then copying 
the jar file in the jre/lib/ext folder.

How to use static import?

The static import feature introduced in Java 5 to access any static member of a class directly. There is no need to qualify a class name.

Static Import Advantage

Less coding is required: if need to use some static members frequently.

Static import Disadvantage

If you overuse the static import statement, then it creates a problem to understand and maintain code.

Example of Static Import

import static java.lang.System.*;

public class StaticImportExample {
	public static void main(String args[]) {
		// Now no need of write System.out
		out.println("Facing Issues on IT");
		out.println("Learn from Other Experinces");



Facing Issues on IT
Learn from Other Experinces

What is the difference between import and static import?

import static import
The import statement allows the java programmer to access classes of a package without package qualification. The static import statement allows accessing the static members of a class without the class qualification.
The import statement provides accessibility to classes and interface. The static import statement provides accessibility to static members of the class.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s