In Java, a Package is used to group related classes, interfaces, and sub-packages. Think of it as a folder on your computer that keeps your files organized. The Java API (Application Programming Interface) is a massive library of pre-written classes that come free with the Java Development Kit (JDK).
As your project grows, you might end up with hundreds of classes. Without packages, managing them would be a nightmare. Packages provide three main benefits:
Date) as long as they belong to different packages (e.g., java.util.Date and java.sql.Date).Java packages are divided into two main categories:
These come with the JDK. Examples include java.lang (core features), java.util (collections), and java.io (input/output).
These are packages created by you to organize your own project classes. You define them using the package keyword at the top of your file.
To use a class from a package, you must tell Java where to find it using the import statement. This happens at the very beginning of your code.
import java.util.Scanner; (Best practice for performance).import java.util.*; (The * is a wildcard that imports everything in that folder).java.lang package into every program. This is why you don't need to import classes like System or String.
The Java API is a library of thousands of classes. To use it effectively, you should familiarize yourself with the official Java API Documentation. It serves as the ultimate manual for every Java developer.
| Package | Description |
|---|---|
java.util |
Contains the collections framework, date/time facilities, and scanners. |
java.io |
Used for system input and output through data streams and serialization. |
java.net |
Provides classes for implementing networking applications. |
java.awt / javax.swing |
Used for creating Graphical User Interfaces (GUIs). |
When creating a user-defined package, follow the Reverse Domain Name convention (e.g., com.yourname.projectname). This ensures your package names are unique worldwide.
Note: The file Calculator.java must be saved in a folder named com/myapp/utils.
package statement must be the first line in the file, followed by import statements, and then the class declaration.import java.util.*; in large projects. It can lead to naming conflicts and makes it harder to see which classes your code actually depends on.java.util.* does not import classes in java.util.zip. You must import sub-packages separately.Q: What is a Static Import?
A: It allows you to access static members of a class (like Math.PI) without using the class name. Example: import static java.lang.Math.*;
Q: Can we have two package statements in one Java file?
A: No. A Java file can belong to only one package.
Q: Difference between 'import package.ClassName' and 'import package.*'?
A: The former imports only one class, while the latter imports all classes within that specific package. Neither affects performance at runtime, but the former is cleaner.
Mastering Packages and the Java API turns you from a coder into an architect. By organizing your own logic into packages and efficiently using the thousands of tools provided in the Standard Java Library, you can build massive, professional-grade applications with ease.
Next: Master Java Inheritance →