Java Packages
Organize types and make dependencies clear.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Packages.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
A package is a namespace for related Java types. An import lets you use a type name without spelling its fully qualified name each time; it does not install a library. Multi-file package layouts are easiest to practice locally. The single-file browser example below demonstrates importing a standard-library class.
Explore the details
Java Packages & API
A package in Java is used to group related classes. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code. Packages are divided into two categories:
- Built-in Packages (packages from the Java API)
- User-defined Packages (create your own packages)
Built-in Packages
The Java API is a library of prewritten classes, that are free to use, included in the Java Development Environment.
The library contains components for managing input, database programming, and much much more. The complete list can be found at Oracles website: https://docs.oracle.com/javase/8/docs/api/.
The library is divided into packages and classes. Meaning you can either import a single class (along with its methods and attributes), or a whole package that contain all the classes that belong to the specified package.
To use a class or a package from the library, you need to use the import
keyword:
Import a Class
If you find a class you want to use, for example, the Scanner class, which is used to get
user input, write the following code:
In the example above, java.util is a package, while Scanner is a class of
the java.util package.
To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation.
In our example, we will use the nextLine() method, which is used to read a
complete line:
Import a Package
There are many packages to choose from. In the previous example, we used the Scanner class from the java.util package. This package also contains date and time
facilities, random-number generator and other utility classes.
To import a whole package, end the sentence with an asterisk sign (*).
The following example
will import ALL the classes in the java.util package:
Common Built-in Java Packages
| Package | What it provides | Example classes |
|---|---|---|
java.lang | Core Java classes — imported automatically | String, Math, System, Integer |
java.util | Data structures, date/time, random numbers | ArrayList, HashMap, Scanner, Date |
java.io | Reading and writing files / streams | File, FileWriter, BufferedReader |
java.nio.file | Modern file & path API (Java 7+) | Files, Path, Paths |
java.net | Networking — URLs, sockets, HTTP | URL, HttpURLConnection |
java.sql | Database connectivity (JDBC) | Connection, Statement, ResultSet |
User-defined Packages
To create your own package, you need to understand that Java uses a file system directory to store them. Just like folders on your computer:
To create a package, use the package keyword:
Save the file as MyPackageClass.java, and compile it:
Example
C:\Users\Your Name>javac MyPackageClass.javaThen compile the package:
Example
C:\Users\Your Name>javac -d . MyPackageClass.javaThis forces the compiler to create the "mypack" package.
The -d keyword specifies the destination for where to save the class file. You
can use any directory name, like c:/user (windows), or, if you want to keep
the package within the same directory, you can use the dot sign ".", like in
the example above.
Note: The package name should be written in lower case to avoid conflict with class names.
When we compiled the package in the example above, a new folder was created, called "mypack".
To run the MyPackageClass.java file, write the following:
Example
C:\Users\Your Name>java mypack.MyPackageClassThe output will be:
Creating Your Own Package — Quick Reference
// Step 1 — Declare the package at the TOP of the file
package com.myapp.utils;
// Step 2 — Define your class
public class StringHelper {
public static String shout(String s) {
return s.toUpperCase() + "!";
}
}
// Step 3 — Use it from another file
import com.myapp.utils.StringHelper;
public class Main {
public static void main(String[] args) {
System.out.println(StringHelper.shout("hello"));
// Output: HELLO!
}
}Naming convention: Package names are always lowercase and use the reverse domain pattern: com.companyname.projectname.module. This guarantees globally unique package names across all Java projects.
A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws Exception {
ArrayList<String> topics = new ArrayList<>();
topics.add("Packages");
System.out.println(topics.get(0));
}
}Predict first. Then reveal the expected output / வெளியீடு
Packages
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Remove the import and replace ArrayList with java.util.ArrayList everywhere in the example.
Reveal the answer and reasoning / விடை
The fully qualified name works without an import. For a local package demo, put package lessons; at the top of Main.java, compile with javac -d . Main.java, and run java lessons.Main.
One mistake to avoid: An import is not a dependency download. A third-party library must already be available to the compiler and runtime.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.