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.
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.
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.
| Type | Examples |
|---|---|
| Predefined | java.lang, java.io, java.awt, java.util, java.net |
| User-defined | Application 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.langjava.iojava.awtjava.utiljava.net
java.lang is automatically available to Java programs.4. java.lang Package
The java.lang package contains commonly required classes and interfaces.
| Type | Members shown in the PDF |
|---|---|
| Classes | String, StringBuffer, Object |
| Interfaces | Runnable, 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.
| Type | Members shown in the PDF |
|---|---|
| Classes | FileInputStream, FileOutputStream, FileReader, FileWriter |
| Interface | Serializable |
java
└── io
├── FileInputStream (class)
├── FileOutputStream (class)
├── FileReader (class)
├── FileWriter (class)
└── Serializable (interface)
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;
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
| Part | Meaning |
|---|---|
javac | Java compiler |
-d | Create the package directory structure |
. | Current working directory |
Test.java | Java source file |
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
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 declares where the source types belong; import makes types from another package available by simple name.10. Set CLASSPATH System Variable
The CLASSPATH variable specifies locations that Java tools can use to find classes and resources.
Display Current CLASSPATH
| Environment | Command |
|---|---|
| Windows | C:\> set CLASSPATH |
| UNIX (Bourne shell) | % echo $CLASSPATH |
Delete Current Contents
| Environment | Command |
|---|---|
| Windows | C:\> set CLASSPATH = |
| UNIX | % unset CLASSPATH; export CLASSPATH |
Set CLASSPATH
| Environment | Command |
|---|---|
| Windows | set CLASSPATH = C:\users\jack\java\classes |
| UNIX | % CLASSPATH = /home/jack/java/classes; export CLASSPATH |
11. Quick Revision
| Concept | Key Point |
|---|---|
| Package | Groups related classes, interfaces, enumerations and annotations. |
| Predefined Package | Supplied by Java, e.g. java.lang and java.io. |
| User-Defined Package | Created by the programmer for application organization. |
| Package Statement | First statement; only one is allowed per source file. |
| Import | Makes types from another package available by simple name. |
javac -d | Creates the package directory structure during compilation. |
| CLASSPATH | Specifies 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.langis automatically available.- Only one package declaration is permitted in a source file.
- The package declaration comes before imports.
javac -dcreates the required package structure.- The generated class files depend on the classes compiled.
- Use the fully qualified class name when executing packaged classes.