What is a Package?
A package in Java is a way to group related classes and interfaces. It helps organize code,
avoid access.A package in Java is a collection of related classes that helps organize code and control
d name conflicts, and control access. Packages in Java also make large projects easier to manage
by grouping related functionality together.
They help prevent naming conflicts when different developers create classes with the same name.
Additionally, packages support encapsulation by restricting access to classes that are meant to be
used only within a specific group.
Purpose of Java Packages
1. To organize code logically by grouping related classes and interfaces.
2. To avoid name conflicts between classes in large projects.
3. To control access to classes, methods, and variables.
4. To make code easier to maintain, read, and reuse.
Advantages of Java Packages
1. Code Organization: Keeps classes in separate folders for better structure.
2. Reusability: Classes inside a package can be reused in other programs.
3. Name Conflict Avoidance: Classes with the same name can exist in different
packages.
4. Access Control: Access modifiers work better with packages (like protected and
default).
5. Maintenance: Easier to update and manage large applications.
6. Modularity: Encourages dividing a program into smaller, independent modules.
Why We Use Packages in Java
1. Organize Code Properly:
Packages keep related classes and interfaces together, making large projects more
structured and easier to manage.
2. Avoid Naming Conflicts:
Two classes with the same name can exist in different packages (e.g.,
[Link] and [Link]).
3. Reusability of Code:
Once a package is created, its classes can be reused in other programs using the
import statement.
4. Access Control:
Packages help control access to classes and methods using access modifiers like
public, protected, and default.
5. Easy Maintenance:
Since related files are grouped together, debugging, updating, or modifying code
becomes simpler.
6. Modularity:
Packages promote modular programming by dividing code into smaller, independent
modules.
7. Improves Readability:
With clear package names, code is easier to understand and navigate.
🧩 Syntax of a Package
package package_name;
The package statement must be the first line in your Java file (before any imports or
class definitions).
The folder structure of your files should match the package name.
💻 Simple Example
Step 1: Create a file named [Link]
package mypackage;
public class Hello {
public void display() {
[Link]("Hello from mypackage!");
}
}
Step 2: Compile the program
javac -d . [Link]
This command creates a folder named mypackage that contains [Link].
Step 3: Create another file named [Link]
import [Link];
public class Test {
public static void main(String[] args) {
Hello obj = new Hello();
[Link]();
}
}
Step 4: Compile and run
javac [Link]
java Test
✅ Output:
Hello from mypackage!
Types of Packages in Java
🧩 1. Built-in Packages
Definition: Predefined packages provided by Java API.
Purpose: To reuse ready-made classes and interfaces without creating them from
scratch.
Common Examples:
o [Link] → String, Math, Object, System (automatically imported)
o [Link] → Scanner, ArrayList, HashMap
o [Link] → File, BufferedReader, FileWriter
o [Link] → URL, Socket
Example using Built-in Package:
import [Link];
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your age:");
int age = [Link]();
[Link]("Your age is " + age);
}
}
🧩 2. User-defined Packages
Definition: Packages created by programmers to organize their own classes.
Purpose: To structure large projects, avoid name conflicts, and promote code reuse.
How to Create: Use package keyword at the top of the file and maintain folder
structure according to package name.
Example of User-defined Package:
[Link]
package mypackage;
public class Hello {
public void display() {
[Link]("Hello from mypackage!");
}
}
[Link]
import [Link];
public class Test {
public static void main(String[] args) {
Hello obj = new Hello();
[Link]();
}
}
Compile and run commands:
javac -d . [Link]
javac [Link]
java Test
✅ Output:
Hello from mypackage!
Quick Summary Table
Feature Built-in Package User-defined Package
Who provides it Java API Programmer
Example [Link], [Link] mypackage, [Link]
Purpose Reuse existing Java classes Organize and reuse own code
Creation required No Yes
Here’s a detailed explanation with an example for putting classes together in a package 👇
🧩 Putting Classes Together in a Package
Concept:
When you have multiple related classes, you can group them in a single package. This
helps:
Organize your project logically
Share code easily between classes
Avoid name conflicts with other classes
Step-by-Step Example
Step 1: Create a Package
Use the package keyword at the top of each Java file.
Folder structure should match the package name.
mypackage/
├── [Link]
└── [Link]
Step 2: Create Multiple Classes
[Link]
package mypackage;
public class Hello {
public void greet() {
[Link]("Hello!");
}
}
[Link]
package mypackage;
public class Bye {
public void farewell() {
[Link]("Goodbye!");
}
}
Explanation:
Both Hello and Bye classes belong to the same package mypackage.
They can be compiled together, and other programs can access them via import
mypackage.*;.
Step 3: Compile the Classes
javac -d . [Link] [Link]
The -d . option creates the mypackage folder and places the .class files inside.
Step 4: Use the Classes in Another File
[Link]
import [Link];
import [Link];
public class Test {
public static void main(String[] args) {
Hello h = new Hello();
[Link](); // Calls Hello class
Bye b = new Bye();
[Link](); // Calls Bye class
}
}
Compile and Run:
javac [Link]
java Test
Output:
Hello!
Goodbye!
✅ Explanation of Example
1. Grouping Classes:
Hello and Bye are related (greeting messages), so we put them together in
mypackage.
2. Accessing Classes:
In [Link], we use import statements to access both classes.
3. Organized Structure:
All related classes are in one folder/package, making the code easy to maintain and
reuse.
4. Package Visibility:
Classes in the same package can access each other using default (package-private)
members if needed.