There are two types of packages in Java: built-in packages and user-defined packages. Built-in packages such as java.lang, java.util and java.io ship with the JDK. User-defined packages are ones you create with the package statement to organise your own application code into named directory structures that prevent class name conflicts.
A package in Java is a namespace that groups related classes, interfaces, enumerations and annotations into a single directory structure. Think of it like a folder on your computer: instead of dumping every .java file into one place, you organise them by purpose. The types of packages in Java fall into two broad categories: built-in packages that ship with the JDK, and user-defined packages you create yourself.
- Key Takeaway 1: A Java package groups related classes and interfaces under a shared namespace, preventing name conflicts across large codebases.
- Key Takeaway 2: Built-in packages like
java.lang,java.utilandjava.ioare part of the Java Standard Library and need no installation. - Key Takeaway 3: You declare a user-defined package using the
packagestatement, which must appear on the very first line of your.javafile. - Key Takeaway 4: The
importstatement lets you use classes from other packages without typing the full qualified name every time. - Key Takeaway 5: Correct package organisation is a basic expectation in Java interviews at companies like TCS, Infosys and Wipro, so getting this right early matters.
What a Package Is
A package definition in Java describes a mechanism for organising a set of related types (classes, interfaces, enumerations and annotation types) under one named group. According to the Oracle Java documentation, packages serve two main purposes: they give you a way to organise related types and they provide access protection and name-space management.
On disk, a package maps directly to a directory. If you declare package com.myapp.utils; at the top of a file, the compiler expects that file to live inside a folder path matching com/myapp/utils/. This is not optional; the compiler enforces it.
What a Java Package Consists Of
A Java package consists of one or more .java source files, each beginning with the same package declaration. Those files compile into .class bytecode files stored in the matching directory structure. You can also bundle a package into a .jar (Java ARchive) file for distribution, which is exactly how the JDK ships its own standard library.
Practically speaking, a well-organised package will contain classes that share a single responsibility. A banking application might have com.bank.accounts for account management classes, com.bank.transactions for payment logic and com.bank.security for authentication. Mixing unrelated classes in one package is one of the most common mistakes junior developers make, and it makes code maintenance painful fast.
Why Packages Matter in Real Projects
The Stack Overflow Developer Survey 2024 consistently places Java in the top ten most-used programming languages globally, with strong adoption in enterprise and Android development. According to NASSCOM’s 2024 Technology Sector Report, Java remains one of the top three languages demanded by Indian IT employers, with over 120,000 active Java developer job postings recorded across major hiring platforms in India. Large Java projects at firms like Infosys or HCL can contain hundreds of classes, and without packages, name collisions become unavoidable. Two libraries might each define a class called Logger, and packages are the only thing keeping them apart.
If you are studying BSc Computer Science subjects, Java is almost certainly on your syllabus, and packages appear in practicals from the second semester onward. Getting comfortable with them early saves a lot of confusion later.
Built-In vs User-Defined Packages in Java
The two types of packages in Java differ in origin and purpose. Built-in packages are pre-written, tested and included with every JDK installation. User-defined packages are ones you write to organise your own application code. Understanding the difference between built-in and user-defined packages in Java is one of the first questions asked in campus placement drives at companies like Wipro, Cognizant and Tech Mahindra.
Built-In Packages in Java
Java’s standard library ships with hundreds of packages. The ones you will use most often as a beginner are listed below. According to Oracle’s Java SE 21 API documentation, the standard library contains over 4,000 classes spread across dozens of packages.
| Package | What It Contains | Common Classes |
|---|---|---|
java.lang |
Core language classes; auto-imported | String, Math, System, Object, Thread |
java.util |
Data structures and utilities | ArrayList, HashMap, Scanner, Date |
java.io |
Input/output operations | File, BufferedReader, FileWriter |
java.net |
Networking | URL, Socket, HttpURLConnection |
java.sql |
Database connectivity (JDBC) | Connection, Statement, ResultSet |
javax.swing |
GUI components | JFrame, JButton, JTextField |
Notice that java.lang is the only package imported automatically. Everything else requires an explicit import statement or a fully qualified class name like java.util.ArrayList.
User-Defined Packages in Java
You create a user-defined package whenever you add the package statement to your source file. The name you choose should follow the reverse-domain convention: if your company’s domain is mycompany.in, your packages start with in.mycompany. This convention is not enforced by the compiler, but every serious Java project follows it because it guarantees global uniqueness.
A student working on a library management system for a college project in Pune might name their packages com.librarymgmt.books, com.librarymgmt.members and com.librarymgmt.reports. It is a small project, but the habit of structuring the types of packages in Java correctly is exactly what interviewers check.
The package and import Statements
The package statement in Java is always the first line of a source file, before any import statements and before the class declaration. It looks like this:
package com.myapp.utils;
If you omit the package statement, Java places the class in the unnamed default package. That is fine for tiny one-file experiments, but it is considered bad practice in any real application. You cannot import classes from the default package into a named package, which creates problems the moment your project grows.
How the import Statement Works
The import statement tells the compiler where to find a class you are using. Without it, you would have to write java.util.ArrayList every single time instead of just ArrayList. You can import a single class or use a wildcard to import all public types from a package:
- Single class import:
import java.util.ArrayList; - Wildcard import:
import java.util.*; - Static import:
import static java.lang.Math.PI;(imports a specific static member)
Wildcard imports do not hurt performance because the compiler resolves which classes are actually used at compile time. They do hurt readability, though. Most professional teams, and tools like Google’s Java Style Guide, recommend explicit single-class imports so anyone reading the code knows exactly what is being used.
The Classpath and Package Resolution
When you compile and run a Java program, the JVM uses the classpath to find your packages. The classpath is a list of directories and .jar files where the JVM looks for compiled .class files. If your class is in com/myapp/utils/StringHelper.class but the classpath does not point to the parent directory, you will get a ClassNotFoundException at runtime. This is one of the most common runtime errors beginners face, and you can read more about how to handle it in our guide to Java exceptions and common runtime errors.
How to Create a User-Defined Package in Java
Creating a user-defined package is straightforward. Here is the exact process, step by step.
- Declare the package on line 1 of your
.javafile. Example:package com.college.grader; - Write your class normally below the declaration. Save the file as
GradeCalculator.javainside the foldercom/college/grader/. - Compile from the project root:
javac com/college/grader/GradeCalculator.java - Run using the fully qualified name:
java com.college.grader.GradeCalculator - Import in another class: Add
import com.college.grader.GradeCalculator;at the top of any file that needs it.
The folder structure on disk must exactly mirror the package name. This is non-negotiable. If the folders do not match, the compiler throws an error immediately. IDEs like IntelliJ IDEA and Eclipse create the folders automatically when you create a new class with a package name, which is one reason learning an IDE early is worth the effort. Our list of software development tools to learn first covers IDEs and other essentials for beginners.
A Practical Java Package Example
Here is a minimal but complete Java package example. Imagine you are building a simple student result system.
File 1: com/results/Student.java
package com.results;
public class Student {
public String name;
public int marks;
public Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
}
File 2: Main.java (in the project root)
import com.results.Student;
public class Main {
public static void main(String[] args) {
Student s = new Student("Priya", 88);
System.out.println(s.name + " scored " + s.marks);
}
}
Compile with javac com/results/Student.java Main.java and run with java Main. The output is Priya scored 88. Clean, organised and fully portable.
Access Modifiers and Package-Level Visibility
Packages interact directly with Java’s access control system. A class or member with no modifier (called package-private or default access) is visible only to other classes in the same package. public makes it visible everywhere. This is a powerful design tool: you can expose a clean public API while keeping implementation details hidden inside the package, which is exactly what the Oracle access control documentation recommends.
Getting this wrong is a common interview trap. Candidates often assume that a class with no access modifier is accessible everywhere, but it is not. Package-private is the default, not public.
If you are preparing for software development roles, understanding how compilation and file organisation work in different languages is useful context. Our tutorial on how to write, compile and run a C program shows how similar concepts apply in C, which helps you see the bigger picture across languages.
For students weighing career paths, strong Java fundamentals open doors in both full stack development and general software engineering. If you are an MCA graduate wondering where packages and core Java fit your career, our breakdown of jobs and salary after MCA gives you realistic benchmarks for the Indian market.
Here is the bottom line on the types of packages in Java: they are not just a syntax requirement, they are the foundation of maintainable code. Every production Java application uses them, every Java interview touches on them, and understanding them makes everything else in the language easier. Your three actionable next steps this week:
- Create a small Java project with at least three user-defined packages and practise compiling it from the command line, not just from an IDE.
- Read through the
java.utilpackage in the Oracle Java SE 21 API docs and note five classes you have not used yet. - If you want to apply Java to cybersecurity, secure coding and ethical hacking, explore 3.0 University’s CEH v13 AI course or browse the full range of 3.0 University online certification courses in AI, Blockchain, Web3 and Cybersecurity, all built for learners who already have programming foundations like the ones you are building right now.
Frequently Asked Questions
What is a package in Java?
A package in Java is a namespace that organises related classes, interfaces and other types into a single group, mapped to a directory on disk. It prevents name conflicts between classes in different libraries and controls access visibility. Every Java file belongs to a package, even if it is the unnamed default package.
What are the types of packages in Java?
There are two types of packages in Java: built-in packages and user-defined packages. Built-in packages like java.lang, java.util and java.io are included with the JDK. User-defined packages are ones you create yourself using the package statement to organise your own application code.
What is the difference between built-in and user-defined packages in Java?
Built-in packages are pre-written by Oracle and ship with every JDK installation, covering core functionality like data structures, I/O and networking. User-defined packages are created by developers to organise their own application code. Both types use the same package and import syntax, but built-in packages require no setup beyond installing the JDK.
What does a Java package consist of?
A Java package consists of one or more .java source files that share the same package declaration at the top. These compile into .class bytecode files stored in a matching directory structure. Packages can also be bundled into .jar files for distribution and reuse across projects.
What is the package statement in Java?
The package statement is the first line of a Java source file and declares which package the class belongs to. Its syntax is package packagename;, for example package com.myapp.utils;. It must appear before any import statements and before the class declaration, and it must match the folder structure on disk exactly.
How do I create a package in Java?
Add package yourpackagename; as the first line of your .java file, then save the file inside a folder whose path matches the package name. Compile from the project root using javac and run using the fully qualified class name. IDEs like IntelliJ IDEA handle the folder creation automatically when you specify a package name while creating a new class.
Last updated: October 2026. Reviewed by the 3University editorial team.