Java Packages

A package in java is a group of similar classes, interfaces, enumerations and sub-packages. It is also used to manage class files in hierarchical file systems to prevent naming conflicts, control access of class and to keep related classes and interfaces together in same directly.

Java programming language provides lots of inbuilt packages for common tasks which we can directly use in our programs like java.lang, java.io, java.util, java.applet, java.swing etc.


Key Concepts of Packages

  • Namespace Management : Packages help avoid naming conflicts by providing a namespace for the classes they contain. Classes within a package share the same namespace, reducing the likelihood of naming clashes.

  • Modularity : Packages facilitate the creation of modular and maintainable code by organizing related classes into coherent units. This enhances code readability and reusability.

  • Access Control : Packages also play a role in access control by defining visibility boundaries. Classes and members marked with the default access modifier are accessible only within the same package.

Advantages of Package in Java
  • As related classes are grouped together in a package, it becomes easy to categorize and search a files.

  • Package provides access protection.

  • Packages avoid namespace collisions. A package creates a new namespace, hence there won't be any namespace conflicts with two classes of same name in different packages.

  • Packages support re-usability of software components. Anyone can import a shared package and include it in his class file and reuse existing classes.

Types of Java Package
  • User Define Packages : These are the packages created by user for his specific requirements. These packages are not part of the standard JDK.

  • Built-in Packages : These are the ready use use, in-built packages provided by JDK like java.lang, java.util etc.

Creating a Package in Java

"package" keyword is used to create a new package. To create a package we have to include a "package" statement specifying the name of the package at top of every interface, classes and enumeration that you want to include in this package.

  • package statement must be the first statement in any source file.
  • Only one package statement is allowed per source file.

However, package statement is optional if we don't specify package in a source file then it will be part of the default package.


Syntax of package statement

package package_name;

Here is an example program with package statement.

package com.tcc.java.tutorial;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Naming Conventions of Package
  • Package name should be in lowercase characters.

  • To ensure that the package name is unique, generally the company url is used starting in backword.

  • For Example : package com.companyname.application.module;

Compiling a Package in Java

The java compiler will place the class file of a class in the directory that corresponds to the package declaration of class.

For Example:
Class Employee is specified package is tcc.JavaPrograms. Then on compilation, Employee.class file will be placed in folder ./tcc/JavaPrograms.

If you want to specify the destination folder location to store the generated .class files then use –d option when compiling with the javac compiler.

javac -d destinationDirectory javaFileName 
For Example:
javac -d /tcc/JavaPrograms Employee.java

Accessing Java Packages from Other Package

If a class wants to access any other class in same package then it can access it directly. Classes in same package can access each other without any package import or special syntax. However, if a class wants to access a class in different package then we have to do one of the following :

  • Using Fully Qualified Name : Here we don't have to import the desired, but we have to use the fully qualified name of the class every time we want to access it.
    For Example:
    tcc.JavaPrograms.Employee emp = new tcc.JavaPrograms.Employee();
    
    NOTE : Using fully qualified names of classes in different package is considered a bad programming practice.

  • By importing the class we want to use : Here, import keyword is used to import classes and packages into your java program file. Once imported your class access imported class by simply using it's name.
    For Example:
    import tcc.JavaPrograms.Employee;
    class Demo {
       Employee e;
    }

  • By importing all the classes of a package : Instead of selectively importing required classes one by one, we can import all the classes and interfaces of a package to our java source file by using package.*
    For Example :
    import tcc.JavaPrograms.*;
    class Demo {
       Employee e;
    }
    Above import statement will make all the classes and interfaces of tcc.JavaPrograms package accessible in current java source file.


Java Standard Packages

Java provides a set of standard packages that offer a wide range of functionalities. Some commonly used standard packages include:
  • java.lang : Core classes and fundamental types.

  • java.util : Utility classes (collections, date, etc.).

  • java.io : Input and output classes.

  • java.net : Networking classes.

  • java.awt and javax.swing : Classes for creating graphical user interfaces (GUI).

  • java.sql : Classes for database access.

Conclusion

Packages in Java are a powerful mechanism for organizing and structuring code. They promote modularity, reduce naming conflicts, and provide a way to manage large codebases effectively. By following naming conventions, creating meaningful package hierarchies, and using access modifiers appropriately, you can enhance the readability, maintainability, and scalability of your Java projects.

Understanding how to create, organize, and use packages is a fundamental skill for Java developers. Whether you are working on small projects or large-scale applications, incorporating packages into your codebase contributes to creating well-structured and maintainable software. As you continue your Java programming journey, mastering the use of packages will be a valuable asset in building robust and scalable applications.