java
- IntroductionInstallation and SetupJava Syntax and StructureRunning Your First ProgramJava VariablesData Types in JavaOperators in JavaJava Input and OutputControl StatementsLoops in JavaMethods in Java (Functions)Arrays in JavaString Handling in JavaOOPS Concept in JavaClasses and ObjectsConstructor and Constructor Overloadingthis KeywordStatic MembersInheritanceAggregation in JavaMethod OverloadingMethod Overridingsuper KeywordFinal Keyword with Class, Method, and VariablesAccess Modifiers in javaEncapsulation in Java Polymorphism in JavaAbstraction in JavaAbstract ClassesInterfaces in JavaDifference between Abstract Class and Interface Nested and Inner Classes Exception HandlingJava PackagesWrapper Classes and AutoboxingJava Collections FrameworkFile Handling in JavaMultithreadingBasics of Java Memory Management and Garbage CollectionJava JDBC
Java Packages – Complete Tutorial
Last Updated on: 24th Nov 2025 18:04:46 PM
In Java programming, applications grow quickly—from a few classes to hundreds or even thousands of files. In large applications, hundreds of classes exist. If everything is placed in one folder, the project becomes messy, confusing, and hard to maintain.
To solve this problem, Java provides Packages, one of the most powerful organizational features of the language.
A Package in Java is like a folder that groups related classes, interfaces, and sub-packages. It helps you structure your project cleanly, avoid name conflicts, and improve code reusability.
What is a Package?
A package in Java is a folder/directory used to group related classes, interfaces, and sub-packages.
Packages help in:
✔ Organizing code
✔ Avoiding class name conflicts
✔ Improving maintainability
✔ Access control (public, protected, default)
✔ Reusability (importing packages)
Types of Packages in Java
Java supports two types of packages:
1. Built-in Packages (Predefined Packages)
Provided by Java.
Examples:
-
java.util → Scanner, ArrayList
-
java.io → File, InputStream
-
java.lang → String, Math, System
-
java.awt → GUI components
-
java.sql → JDBC
2. User-Defined Packages
Created by the programmer.
Example:
package mypackage;
Using Built-in Packages (Example)
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter name:");
String name = sc.nextLine();
System.out.println("Hello " + name);
}
}
Why Use Packages? (Real-Life Scenario)
Suppose iKeySkills is creating a big Java course platform with:
-
User management
-
Payment system
-
Courses
-
Certificates
-
Admin panel
Each module can be placed in a separate package:
com.ikeyskills.user
com.ikeyskills.payment
com.ikeyskills.courses
com.ikeyskills.admin
This keeps the project clean and maintainable.
Creating User-Defined Packages (Step-by-Step Practical)
Follow this easy practical tutorial.
Step 1: Create Folder Structure
Create a main folder:
MyProject
└── src
└── com
└── ikeyskills
└── courses
└── CourseInfo.java
Step 2: Create the Package File
File: CourseInfo.java
package com.ikeyskills.courses;
public class CourseInfo {
public void display() {
System.out.println("Java Course: Beginner to Advanced");
}
}
The first line must be package com.ikeyskills.courses;
This tells Java the class belongs to this package.
Step 3: Create Another Class in a Different Folder
File: Main.java (outside the package)
MyProject
└── src
└── Main.java
import com.ikeyskills.courses.CourseInfo;
public class Main {
public static void main(String[] args) {
CourseInfo c = new CourseInfo();
c.display();
}
}
Use import to bring the package class into the program.
Or use wildcard: import com.ikeyskills.courses.*; for all classes in package.
Step 4: Compile Package Files (IMPORTANT)
Go to the src folder in terminal:
cd MyProject/src
Compile the package class:
javac com/ikeyskills/courses/CourseInfo.java
Compile Main.java:
javac Main.java
Step 5: Run the Program
java Main
Output
Java Course: Beginner to Advanced
Creating Sub-Packages (Nested Packages)
Folder structure:
com
└── ikeyskills
└── payment
└── gateway
└── Razorpay.java
File: Razorpay.java
package com.ikeyskills.payment.gateway;
public class Razorpay {
public void pay() {
System.out.println("Payment done using Razorpay!");
}
}
Main Class:
import com.ikeyskills.payment.gateway.Razorpay;
public class Main {
public static void main(String[] args) {
new Razorpay().pay();
}
}
Access Modifiers in Packages
| Access Modifier | Same Class | Same Package | Subclass (Other Package) | Other Package |
|---|---|---|---|---|
| public | ✔ | ✔ | ✔ | ✔ |
| protected | ✔ | ✔ | ✔ | ❌ (except through inheritance) |
| default | ✔ | ✔ | ❌ | ❌ |
| private | ✔ | ❌ | ❌ | ❌ |
Packages affect access level:
| Modifier | Package Access |
|---|---|
| public | Accessible everywhere |
| protected | Accessible in same package + other package but only by subclasses |
| default (no modifier) | Accessible only in same package |
| private | Inside same class only |
Using Fully Qualified Name (No import needed)
Example
public class Main {
public static void main(String[] args) {
com.ikeyskills.courses.CourseInfo c = new com.ikeyskills.courses.CourseInfo();
c.display();
}
}
Good when two classes have the same name.
Advantages of Packages
| Benefit | Explanation |
|---|---|
| Organized Code | Logical grouping |
| Avoid Name Collisions | Same class names in different packages |
| Security | Access control |
| Reusability | Easier to reuse code |
| Maintainability | Easier management of large applications |
Java API Package Summary Table
| Package | Use |
|---|---|
| java.lang | Basic classes |
| java.util | Collections, Scanner |
| java.io | File & input/output |
| java.net | Networking |
| java.sql | Database |
| java.awt | GUI |
| javax.swing | GUI |
Final Summary
✔ A package is a folder structure used to organize Java classes
✔ Two types: built-in & user-defined
✔ Use import to access classes from another package
✔ Folder names = package names
✔ Helps maintain clean, structured, secure code in large projects
Keep practicing — you're doing amazing!
Happy Coding! ![]()
.png)