Package Structure
Until now, we have written all our code in a single file. However, real programs consist of hundreds or thousands of files. The way to systematically organize these files is through Packages.
In Java, a package is more than just folder organization. It is a core design philosophy of the language itself.
What is a Package?
A package is a way to group related classes (files) into a single folder.
src/
└── com/
└── myapp/
├── Main.java
├── Calculator.java
└── utils/
└── Helper.java
The very first line of every Java file must declare which package it belongs to:
package com.myapp;
void main() {
System.out.println("Hello!");
}
package com.myapp; tells Java, “This file is located in the com/myapp folder.”
Why should we use Packages?
1. Organization
Once you have over 100 files, you can’t put them all in one folder. You need to categorize them by their roles.
2. Preventing Name Conflicts
What if two people both created a file named User.java? If they are in different packages, there is no problem:
com.myapp.model.Usercom.myapp.admin.User
Even though they have the same name, they can be distinguished because their addresses (packages) are different.
Using Classes from Other Packages
To use a class that is in a different package, you use import:
package com.myapp;
import com.myapp.utils.Helper;
void main() {
Helper.doSomething();
}
import means “I will bring in and use this class.”
In IntelliJ, it automatically adds imports when you use a class from another package, so you don’t have to worry too much!
Java’s Standard Packages
The System.out.println() we’ve been using is actually part of the java.lang package.
Java makes an exception for the java.lang package, allowing us to use it without an explicit import.
Commonly used standard packages:
| Package | Description |
|---|---|
java.lang | The most basic tools (String, System, etc.) - No import needed |
java.util | Data structures like Lists and Maps |
java.io | Reading and writing files |
java.math | Math-related functions |
import java.util.ArrayList;
import java.util.HashMap;
This is how you pick and use the tools you need.
What a Real Project Looks Like
src/
└── com/
└── myapp/
├── Main.java ← Program entry point
├── model/ ← Classes that hold data
│ ├── User.java
│ └── Product.java
├── service/ ← Business logic
│ ├── UserService.java
│ └── ProductService.java
└── util/ ← Toolset
└── StringHelper.java
By dividing packages by role, you can immediately find what you’re looking for: “User-related logic will be in the service package, and data structures will be in the model package!”
The Secret of void main() and System.out.println()
Now we can finally explain the code we’ve been using since the beginning.
void main()
void main() {
System.out.println("Hello!");
}
- void: This means “this code does not return a result.” As you learn more about functions, you’ll create functions that return results like
intorString. - main: This is the name Java looks for first when starting a program. Think of it as the “front gate” of the program.
Java always finds the gate named main and executes the code inside it. That’s why every Java program must have a main.
System.out.println()
This long line of code can also be understood through the concept of packages:
System.out.println("Hello!");
- System: A class in the
java.langpackage. It’s a tool for talking to the computer system. - out: The “output” tool inside
System. - println(): A function that tells
outto “print this and move to a new line.”
In other words, System.out.println("Hello!") means “Use the system’s output tool to print ‘Hello!’ and move to the next line.”
While other languages use something simple like print("Hello!"), Java is longer because it organizes everything systematically into packages and classes. It might feel tedious at first, but this structure shines as programs grow larger.
To summarize:
- Package = A folder to organize related Java files
- package declaration = Telling Java where this file belongs
- import = Bringing in classes from other packages
- Standard Package = A collection of tools pre-made by Java
In Java, packages are not optional—they are essential. Developing a habit of organizing your code well is the first step toward becoming a great Java developer!