Skip to content

Create and Access Packages

Packages in Java help organize related classes and interfaces, provide access control, and avoid naming conflicts. Below is a detailed guide on creating and accessing packages in Java.


Step 1: Create a Package

  1. Define the Package Name:
    1. Use the package keyword at the top of your Java file.
  2. Save the File in a Folder Matching the Package Name:
    1. The folder name should match the package name.

Example: Creating a Package

// File: MyPackage/MyClass.java

package MyPackage; // Package declaration

public class MyClass {

    public void displayMessage() {

        System.out.println(“Hello from MyClass in MyPackage!”);

    }

}

  • Save the file as MyClass.java in a directory named MyPackage.

Step 2: Compile the Package

Use the -d option to specify the base directory for the package structure.

javac -d . MyPackage/MyClass.java

  • This creates the MyPackage directory (if it doesn’t exist) and places the compiled .class file inside it.

Step 3: Access the Package

To access the package, you need to import it or use the fully qualified class name.

  1. Import the Package:
    1. Use the import statement to include the package.
  2. Use the Class:
    1. Create objects of the class and call its methods.

Example: Accessing the Package

Main Class

// File: Main.java

import MyPackage.MyClass; // Importing the package

public class Main {

    public static void main(String[] args) {

        MyClass obj = new MyClass(); // Using the class from MyPackage

        obj.displayMessage();

    }

}

Steps to Compile and Run

  1. Compile:
    1. Compile the Main.java file.

javac -d . Main.java

  • Run:
    • Run the program using:

java Main

Output:

Hello from MyClass in MyPackage!


Accessing Without import

You can also access the package by using the fully qualified class name:

public class Main {

    public static void main(String[] args) {

        MyPackage.MyClass obj = new MyPackage.MyClass(); // Fully qualified name

        obj.displayMessage();

    }

}


Working with Sub-Packages

Java allows creating sub-packages for better modularity.

Example: Sub-Package

  1. Create a Sub-Package:

// File: MyPackage/SubPackage/SubClass.java

package MyPackage.SubPackage;

public class SubClass {

    public void show() {

        System.out.println(“Inside SubPackage”);

    }

}

  • Access the Sub-Package:

import MyPackage.SubPackage.SubClass;

public class Main {

    public static void main(String[] args) {

        SubClass obj = new SubClass();

        obj.show();

    }

}

  • Compile:

javac -d . MyPackage/SubPackage/SubClass.java

javac -d . Main.java

  • Run:

java Main

Output:

Inside SubPackage


Advantages of Using Packages

  1. Code Organization:
    1. Packages group related classes, making the project structure cleaner.
  2. Avoid Name Conflicts:
    1. Classes with the same name can exist in different packages.
  3. Code Reusability:
    1. Packages can be imported into multiple projects.
  4. Access Control:
    1. Packages work with access modifiers to control visibility.

Best Practices for Package Naming

  1. Follow Naming Conventions:
    1. Use all lowercase letters for package names.
    1. Reverse domain name for unique global packages:

package com.example.myproject;

  • Use Logical Grouping:
    • Group related classes into meaningful packages (e.g., utils, models, services).

Conclusion

Creating and accessing packages in Java is straightforward and essential for building modular, scalable, and maintainable applications. By organizing code into packages, you ensure a cleaner structure, better reusability, and minimal conflicts across your codebase.