Java packages with examples

A package in Java is a mechanism for grouping related classes, interfaces, and sub-packages. A package can be thought of as a folder containing a collection of related files. It aids in the avoidance of naming conflicts between classes and the creation of a code hierarchy.

Java packages are identified by a naming convention that reverses the domain name. If a company's domain name is "codescracker.com," the package name should be "com.codescracker."

Simply include the "package" statement at the start of the source file to create a package. For instance, you would start your Java source file with the following statement to create a package called "com.codescracker.mypackage":

package com.codescracker.mypackage;

Import a package to use its classes or interfaces in your code. To import "MyClass" from "com.codescracker.mypackage" into your Java source file, use the "import" keyword in this way:

import com.example.mypackage.MyClass;

Define a package in Java

The "package" keyword is used at the start of a source file in Java, followed by the package name, to define a package. For illustration, the code below creates the "com.codescracker" package:

package com.codescracker;

public class MyClass {
  // class implementation
}

The "MyClass" class is defined in the "com.codescracker" package in the above code fragment. To use a class from a package in another source file, we need to import that package using the "import" keyword, of course. As an example, if we have a class named "MyClass" in the "com.codescracker" package, we can use it in another source file like this:

import com.codescracker.MyClass;

public class XYZ {
  public static void main(String[] args) {
    MyClass obj = new MyClass();
    // block of code to process further
  }
}

Java package example

Consider the following program as an example demonstrating the use of packages in Java. But before we get into the example, first create a new folder with any name, let's say "xyzPackage" in the project's root directory. Then create "MyClass.java" and "Main.java" in the "xyzPackage" folder. Consider the following code for the "MyClass.java" file.

package xyzPackage;

public class MyClass {
    public void myMethod() {
        System.out.println("Hello, I'm Edwin.");
    }
}

And consider the following code for the "Main.java" file.

package xyzPackage;

public class Main {
    public static void main(String[] args) {
        MyClass myObject = new MyClass();
        myObject.myMethod();
    }
}

In this example, I create a package called "xyzPackage" and a class called MyClass within it. Then we write a Main class that makes use of the MyClass from the same package.

It is now time to compile and run this Java example program demonstrating the use of package. To compile and run this program, go to the "xyzPackage" parent directory and run the following commands:

javac xyzPackage/*.java
java xyzPackage.Main

Advantages of packages in Java

Disadvantages of packages in Java

Java Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!