Java Fundamentals

Packages in Java

Prepared by Srikanth Mamillapalli

1. Packages in Java

A package contains a group of related classes and interfaces. It is an encapsulation mechanism that binds related types and provides a clear physical directory structure for project modules.

  • Groups related classes and interfaces.
  • Provides separation between project modules.
  • Improves organization, reuse and maintainability.
  • Creates a namespace and helps prevent naming conflicts.
  • Makes related classes and interfaces easier to locate and use.
  • Provides access protection.
Definition: A package is a grouping of related types such as classes, interfaces, enumerations and annotations, providing namespace management and access protection.

Package Statement

package com.example.demo;

The package statement should be the first statement in the source file. Only one package statement can occur in a source file, and it applies to all types in that file. Without a package statement, the types belong to the default package.

Packages overview from PDF

2. Types of Packages

Predefined Packages

Provided by Java and containing commonly used classes and interfaces.

User-Defined Packages

Created by programmers to organize their own related classes and interfaces.

TypeExamples
Predefinedjava.lang, java.io, java.awt, java.util, java.net
User-definedApplication packages such as com.tcs.onlineexam

3. Predefined Packages

Java predefined packages are supplied as part of the Java platform and contain ready-to-use classes and interfaces.

  • java.lang
  • java.io
  • java.awt
  • java.util
  • java.net
Important: java.lang is automatically available to Java programs.

4. java.lang Package

The java.lang package contains commonly required classes and interfaces.

TypeMembers shown in the PDF
ClassesString, StringBuffer, Object
InterfacesRunnable, Cloneable
java
└── lang
    ├── String (class)
    ├── StringBuffer (class)
    ├── Object (class)
    ├── Runnable (interface)
    └── Cloneable (interface)

Note: java.lang is available by default whether or not it is explicitly imported.

5. java.io Package

The java.io package contains classes used for input/output operations.

TypeMembers shown in the PDF
ClassesFileInputStream, FileOutputStream, FileReader, FileWriter
InterfaceSerializable
java
└── io
    ├── FileInputStream (class)
    ├── FileOutputStream (class)
    ├── FileReader (class)
    ├── FileWriter (class)
    └── Serializable (interface)
java.lang and java.io package diagrams

6. User-Defined Packages

Packages declared by the programmer are called user-defined packages.

  • A source file can contain only one package declaration.
  • The package declaration should be at the beginning of the source file.
  • Package names should normally use lowercase letters.

Valid

package pack1;
import java.lang.*;

Another Valid Example

package pack2;
import java.io.*;
import java.net.*;

Invalid

package pack1;
package pack2;
import java.io.*;
import java.lang.*;
package pack3;
Remember: The package statement must come before import statements and type declarations.
User-defined package examples from PDF

7. Compiling Java Programs with Packages

When a source file contains a package statement, use the -d option with javac to create the required package directory structure and place compiled class files in the specified destination.

Syntax

javac -d Destination_folder file_name.java

Example

javac -d . Test.java
PartMeaning
javacJava compiler
-dCreate the package directory structure
.Current working directory
Test.javaJava source file
javac -d command diagram from PDF

8. Package Directory Structure

After compilation, the directory structure follows the package hierarchy.

Com
└── dss
    └── bank
        └── deposit
            └── *.class

The total number of generated .class files depends on the number of classes present in the source file.

Online Exam Example

Com
└── tcs
    └── OnlineExam
        └── corejava
            ├── Test.class
            ├── A.class
            ├── B.class
            └── C.class

Execution

java com.tcs.onlineexam.Test
Key idea: The package hierarchy forms part of the fully qualified class name.
Package directory structure from PDF

9. Import Session

The main purpose of the import statement is to make Java predefined or other package types available to the program by their simple names.

Example 1

import java.lang.String;

String is a predefined class. Normally java.lang is automatically available.

Example 2

import java.awt.*;

The wildcard form imports accessible types from the specified package.

Package vs Import: package declares where the source types belong; import makes types from another package available by simple name.
Import session from PDF

10. Set CLASSPATH System Variable

The CLASSPATH variable specifies locations that Java tools can use to find classes and resources.

Display Current CLASSPATH

EnvironmentCommand
WindowsC:\> set CLASSPATH
UNIX (Bourne shell)% echo $CLASSPATH

Delete Current Contents

EnvironmentCommand
WindowsC:\> set CLASSPATH =
UNIX% unset CLASSPATH; export CLASSPATH

Set CLASSPATH

EnvironmentCommand
Windowsset CLASSPATH = C:\users\jack\java\classes
UNIX% CLASSPATH = /home/jack/java/classes; export CLASSPATH

11. Quick Revision

ConceptKey Point
PackageGroups related classes, interfaces, enumerations and annotations.
Predefined PackageSupplied by Java, e.g. java.lang and java.io.
User-Defined PackageCreated by the programmer for application organization.
Package StatementFirst statement; only one is allowed per source file.
ImportMakes types from another package available by simple name.
javac -dCreates the package directory structure during compilation.
CLASSPATHSpecifies locations used to find classes and resources.

Package Workflow

Source File
    ↓
package statement
    ↓
javac -d Destination file.java
    ↓
Package directory structure
    ↓
.class files
    ↓
java fully.qualified.ClassName

Key Points to Remember

  • Packages prevent naming conflicts through namespaces.
  • They organize related project types and improve maintainability.
  • java.lang is automatically available.
  • Only one package declaration is permitted in a source file.
  • The package declaration comes before imports.
  • javac -d creates the required package structure.
  • The generated class files depend on the classes compiled.
  • Use the fully qualified class name when executing packaged classes.