πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

Packages in Java  


Introduction
  • A package in Java is a core concept used to group related classes, interfaces and sub-packages.
  • It is used to avoid name conflicts, group related code and improve code maintainability and access control.
  • You can think of a package as a folder in a file system, where similar Java files (classes or interfaces) are stored together β€” just like organizing documents into folders for easy access and management.
  • Java has two types of packages:
    1. Built-in Packages (like java.util, java.io, java.lang)
    2. User-defined Packages (created by the programmer)
  • Why Use Packages?
    • Organizes classes logically (e.g., utility classes, model classes).
    • Avoids class name conflicts between different modules or developers.
    • Provides access protection (using public, protected, private).
    • Makes searching, locating, and using classes/interfaces easier.
  • Syntax to Declare a Package:
    • package package_name;
      • This should always be the first statement in our Java file.
  • Syntax to Import a Package:
    • 1. import package_name.class_name;
      • It is used to import only a specific class from a package.
    • 2. import package_name.*;
      • It is used to import all classes from a package.
    • Import statement should always be the first statement after the package declaration.

1. Built-in Packages:
  • Built-in packages are part of the Java Standard Library and provide ready-made classes and interfaces for various functionalities like data structures, input/output, networking, GUI, and more.
  • Common built-in packages include:
    • java.lang (automatically imported, contains core classes like String, Math, Object)
    • java.util (for collections, Date, etc.)
    • java.io (for input/output streams)
  • Java Program Example:
    import java.util.Scanner;
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter your name: ");
            String name = sc.nextLine();
            System.out.println("Hello, " + name);
        }
    }
    

2. User-defined Packages:
  • User-defined packages are created by programmers to logically group related classes and interfaces, improving code organization, reusability, and avoiding naming conflicts.
  • Java Program Example:
    package p1;
    
    public class MyClass
    {
        public void display()
        {
            System.out.println("Hello from MyClass in p1 package.");
        }
    }
    package p2;
    
    import p1.MyClass;  // Importing MyClass from p1 package
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            MyClass obj = new MyClass();
            obj.display();
        }
    }
  • How to Compile and Run User-Defined Package (using terminal)
    • javac p1/MyClass.java      # Compile the package class
      javac -cp . p2/MainApp.java      # Compile the main class using current dir as classpath
      java p2.MainApp     # Run the program