SrcForge

Lesson 19

Packages

Packages group related classes and interfaces into a structured namespace, improving organization, preventing naming conflicts and promoting reusability.

Java Track

Save progress as you learn.

61 lessons

Lesson content

Packages organize related classes, interfaces and sub-packages into a structured namespace. They prevent naming conflicts and promote code reusability.

Definition List

  • Package: A namespace that groups related classes, interfaces and sub-packages together.
  • Built-in Package: A package provided by Java's standard library.
  • User-Defined Package: A package created by programmers to organize their own classes.
  • Import Statement: A statement used to access classes from another package.
  • Namespace: A container that helps avoid naming conflicts between classes.

Types of Packages in Java

  • Built-in Packages
  • User-Defined Packages

Common Built-in Packages

  • java.lang — Core Java classes
  • java.util — Utility classes and collections
  • java.io — Input and output operations
  • java.net — Networking classes
  • java.awt — GUI components
  • javax.swing — Advanced GUI development
  • java.sql — Database connectivity
  • java.applet — Applet development

Import Syntax

// Import a specific class
import package.name.ClassName;

// Import all classes in a package
import package.name.*;

// Examples
import java.util.*;
import java.applet.Applet;

How to Create a User-Defined Package

  1. Create a folder with the desired package name.
  2. Create a Java file inside the folder.
  3. Declare the package at the top of the file.
  4. Save the file using the public class name.

Example 01: Creating a User-Defined Package

package pack1;

public class Demo {
    public void show() {
        System.out.println("Package called");
    }
}

Using a User-Defined Package

import pack1.*;

class A {
    public static void main(String args[]) {
        Demo d = new Demo();
        d.show();
    }
}

Output

Package called

Best Practices

  • Use meaningful lowercase package names.
  • Follow naming conventions: com.company.project
  • Group classes with similar functionality.
  • Import only required classes when possible.

FAQ

1. What is a package in Java?

A collection of related classes, interfaces and sub-packages.

2. Why are packages used in Java?

To organize code, improve reusability, prevent naming conflicts and control access.

3. What is the difference between built-in and user-defined packages?

Built-in packages come with Java. User-defined packages are created by programmers.

Packages group related classes into a structured namespace.
Java has two package types: built-in and user-defined.
Use the package keyword to declare a package.
Use import to access classes from other packages.
java.util, java.io, java.lang are common built-in packages.
Package names follow lowercase naming conventions.
Packages prevent class name conflicts across projects.

Where Java is used

Java in real-world development

Built-in Packages

Provided by Java's standard library. Examples: java.util, java.io, java.lang, java.sql.

User-Defined Packages

Created by programmers. Declared with the package keyword at the top of the file.

Import Statement

Use import package.name.* to access all classes or import a specific class directly.

Package Declaration

Must be the first statement in a Java file. Example: package com.company.project;

Naming Conventions

Package names are written in lowercase. Use reverse domain format: com.company.module.

Access Control

Packages work with access modifiers to restrict or allow class and member visibility.