Tag: java packages

Packages in Java, Usage of Packages – Part 2 of Parts 2


Naming conventions:

Companies use their reversed Internet domain name in their package names, like this:


How to Use a Package:

Calling it by its full name:

For example,

Com.package1.package2 myNewClass = new com.package1.package2();

This type of calling is very time consuming as it is long. It is recommended to use the second method.

Calling with import:

Use “import” keyword to access packages. In program when you write import com.package1.package2, then every time we can type only “package2″, then compiler can understand that we are calling it as com.package1.package2.
Example illustration is best to understand:

import com.package1.package2;
class PackageDemo {
package2 myNewClass= new package2 ();


Ways of importing a package:

We can import a package in two ways in to our program when we are coding.
That is, One way is to Package Importing only a single member.
//here ‘subclass’ is a java file in myPackage2

import com.package1.package2.childClass;
class PackageDemo {
childClass myNewClass= new childClass();


Second way is, Importing all members of a package.
import com.package1.*;
import java.sql.* ;
When we use *, only the classes in the package referred are imported, and not the classes in the sub package.
For every execution of the Java program, JRE automatically imports two entire packages by default:

The java.lang package java library and the current user defined package by default.

Points to remember:

Sometimes class name conflict may occur. For example:

There are two packages package1 and package2. Assume that both of these packages contains a class “PackageDemo” with the same name, let it be PackageDemo.java. Now both this packages are imported by some other class.

import package1.*;

import package2.*;

The above statements will cause compilation error, because compiler will be in confusion bcz there are two classes with same in two different packages which are imported in the current program. To avoid these naming conflicts in such a situation, we have to be more specific and use the member’s qualified name to indicate exactly which PackageDemo.java class we want:

package1.PackageDemo myNewClass1 = new package1.PackageDemo();

Package2.PackageDemo myNewClass2 = new package2.PackageDemo();

While creating a package, which needs some other packages to be imported, the package statement should be the first statement of the program, followed by the import statement.

Compiling package:

The java compiler can place the byte codes in a directory that corresponds to the package declaration of the compilation unit. The java byte code for all the classes(and interfaces) specified in the source files PackageDemo1.java and PackageDemo2.java will be placed in the directory named package1/package2 , as these sources have the following package declaration

package  package1.package2;

The absolute path of the package1/package2 directory is specified by using the –d (destination directory) option when compiling with the javac compiler.

Assume that the current directory is /packages/project and all the source files are to be found here,the command,

javac –d .file1.java file2.java

Issued in the working directory will create  ./ package1/package2(and any sub directories required) under the current directory, and place the java byte code for all the classes(and interfaces) in the directories corresponding to the package names. The dot (.) after the –d option denotes the current directory. Without the –d option, the default behaviors of  the java compiler is to place all the class files in the current directory rather than the appropriate sub directories.


Run the Program with Packages:

Since the current directory is /packages/project and we want to run file1.java,the fully qualified name of the file1 class must be specified in the java command,

java package1.package2.file1

Classpath :

It is a environmental variable, which contains the path for the default-working directory (.).

The specific location that java compiler will consider, as the root of any package hierarchy is, controlled by Classpath

Compile java 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 run java package program

You 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.


Packages in Java, Introduction – Part 1 or Parts 2



  1. A java package is a group of similar types of classes, interfaces and sub-packages.
  2. A sub-package is again group of similar type of classes, interfaces and sub-packages.
  3. Pakcage in java can be categorized in two types,
    1. Built-in package
    2. and User-defined package

Advantages of Java Package

  1. Easy to Maintain: Java package is used to categorize the classes and interfaces so that they can be easily maintained.
  2. Easy to organize: It is easy to organize class files into packages.
  3. Provide Protection: Java package provides access protection.
  4. Avoid Collision: Java package removes naming collision.
  5. Avoid Conflicts: Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.
  6. Reusability: most important requirements in the software industry. Reusability saves time, effort and also ensures consistency. A class once developed can be resued by any number of programs wishing to incorporate the class in that particular program.

Types of package:

User defined package:

User-defined packages are created by user as project demands based on its requirements. The package we create is called user-defined package.

Built-in package: The already defined package like java.io.*, java.lang.* etc are known as built-in packages.

There are many built-in packages in Java JDK such as java, lang, awt, javax, swing, net, io, util, sql etc

Some of the existing packages in Java are::

  • java.lang – bundles the fundamental classes
  • java.io – classes for input , output functions are bundled in this package
  • Java.util.

Where do we include Package in a typical Java program:

This statement should be used in the beginning of the program to include that program in that particular package.


package tools;

public class Employee {
public void empName ()
System.out.println (“Hello there”);

  1. At most one package declaration can appear in a source file.
  2. The package declaration must be the first statement in the unit.