Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Package

In Java, a package is a collection of related classes and interfaces organized in a namespace. Packages help to avoid naming conflicts, provide better organization of code, and improve code reusability. In this tutorial, we will cover the basics of Java packages, including creating packages, importing packages, and using package access modifiers.

  • Creating a package:

To create a package, you need to add a package declaration as the first non-comment line in your Java source file. The package declaration follows the syntax package packageName;. The package name should be in lowercase and follow the reverse domain name convention.

Example:

// File: com/example/animals/Animal.java
package com.example.animals;

public class Animal {
    // Class implementation
}
  • Importing a package:

When you want to use a class or interface from another package, you need to import it using the import statement. The import statement should be placed after the package declaration and before the class or interface definition.

There are two ways to import classes or interfaces from a package:

  • Single-type import: Import a specific class or interface using the syntax import packageName.ClassName;.
import com.example.animals.Animal;

public class Main {
    public static void main(String[] args) {
        Animal animal = new Animal();
        // ...
    }
}
  • On-demand import: Import all classes and interfaces from a package using the syntax import packageName.*;.
import com.example.animals.*;

public class Main {
    public static void main(String[] args) {
        Animal animal = new Animal();
        // ...
    }
}
  • Using package access modifiers:

Java provides four access modifiers to control the visibility of class members (variables, methods, and inner classes): public, private, protected, and package-private (default). Package-private members are accessible only within the same package and do not require any explicit access modifier.

Example:

// File: com/example/animals/Animal.java
package com.example.animals;

class Animal {
    // Class implementation
}

// File: com/example/animals/Dog.java
package com.example.animals;

public class Dog extends Animal {
    // Class implementation
}

In the example above, the Animal class has a package-private access modifier, meaning it is only accessible within the com.example.animals package. The Dog class, which is in the same package, can access and extend the Animal class.

In this tutorial, we discussed the basics of Java packages, including creating packages, importing packages, and using package access modifiers. Packages are an essential part of organizing your code, avoiding naming conflicts, and improving code reusability in your Java applications.

  1. Creating and Using Packages in Java:

    • Packages are used to organize classes into namespaces. To create a package, simply include a package statement at the top of your Java source file.
    // File: MyPackageClass.java
    package com.example.mypackage;
    
    public class MyPackageClass {
        // Class implementation
    }
    
  2. Java Package Naming Conventions:

    • Package names are usually in lowercase, and it is a common practice to use reverse domain names to avoid naming conflicts.
    package com.example.util;
    
  3. Import Statement in Java for Packages:

    • The import statement is used to bring classes from other packages into the current source file.
    import com.example.mypackage.MyPackageClass;
    
    public class AnotherClass {
        MyPackageClass myObj = new MyPackageClass();
    }
    
  4. Java Package Structure and Organization:

    • Packages are organized hierarchically, and the directory structure mirrors the package structure.
    src/
    ������ com
        ������ example
            ������ mypackage
                ������ MyPackageClass.java
    
  5. How to Organize Classes in Java Packages:

    • Place related classes in the same package to promote organization and maintainability.
    src/
    ������ com
        ������ example
            ������ util
                ������ StringUtil.java
                ������ NumberUtil.java
    
  6. Java Package Visibility and Access Modifiers:

    • Classes in the same package have default visibility to each other. Access modifiers (public, protected, private) control visibility outside the package.
    // In StringUtil.java
    package com.example.util;
    
    public class StringUtil {
        // Class implementation
    }
    
  7. Java Package-Private (Default) Access:

    • Package-private (default) access means a class or member is only accessible within its own package.
    // In StringUtil.java
    package com.example.util;
    
    class StringUtil {
        // Package-private class
    }
    
  8. Java Package Hierarchy and Directory Structure:

    • The package hierarchy is reflected in the directory structure. The root directory is often the src directory.
    src/
    ������ com
        ������ example
            ������ util
                ������ StringUtil.java
    
  9. Java Packages and Classpath:

    • The classpath is a set of directories and JAR files where Java looks for classes. Packages help organize classes within this classpath.
    javac -d . MyPackageClass.java
    java com.example.mypackage.MyPackageClass