Packages & Imports
A real Java project has hundreds of classes. Packages are the folders that keep them organised and namespaced, and imports are how one file borrows a class from another. You've used import dozens of times already — now let's understand exactly what it does.
Learn Packages & Imports in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You should know how to write a class , and it helps to have seen access modifiers — package-private access is defined in terms of packages.
💡 Analogy: A package is a postal address for a class. There might be a hundred people called "Smith" in the world, but only one lives at com.shop.model.Smith . The simple name ( Product ) is the first name; the package ( com.shop.model ) is the full address. An import is you saving a contact so you can call them by first name instead of reciting their full address every time.
By convention package names use your organisation's reversed domain — com.google.common , org.apache.commons — which guarantees globally unique names. And the package name maps exactly to the folder path: com.shop.model means the file sits in com/shop/model/ .
The package statement says which package a file belongs to. It must be the first line of code in the file (comments above it are fine), and there can be only one. The file's folder path has to match the package name.
Here's a class declared in a package. Note the very first line:
To use a class from another package, you import it — then you can refer to it by its short name. Imports go after the package line and before the class. The example below imports your own Product class plus two from the standard library.
An import is just a shortcut. You can always skip it and write the fully qualified name — the package plus the class — inline. You rarely want to, but there's one case where it's the cleanest solution: when two classes share a simple name (the classic clash is java.util.Date vs java.sql.Date ). You can only import one, so you write the other one's full name.
A normal import brings in a class . A import static brings in a class's static members so you can use them without the class prefix. It shines in math-heavy code and test assertions.
A wildcard import ( import java.util.*; ) brings in every public class from a package. It has no runtime cost — the compiler only links the classes you actually use — but it hides exactly which classes you depend on, and it can create ambiguity when two wildcards both contain a class with the same simple name. Most teams prefer explicit single imports (and IDEs auto-manage them), but wildcards are common in quick scripts and examples.
These are the top lines of a Java source file. Java requires them in a strict order — put them right.
The strict rule: comments may sit anywhere, but the package statement must be the first line of code , then all imports, then the class. Swapping the package and import lines is a compile error.
Answer: ArrayList and Scanner need an import (both from java.util ). String and Math live in java.lang , which is imported automatically, so they need no import.
Answer: No. The package statement must come before any import. Here the import is above it, so you get error: class, interface, enum, or record expected / a misplaced-package error. Swap the first two lines.
Answer: It prints com.demo.util , and the source file must live at com/demo/util/Greeter.java — the package name maps directly to the folder path.
🎯 Your Turn #1 — Add the missing imports
This file won't compile because its imports are missing. Add the right import lines at the top.
🎯 Your Turn #2 — Switch to static imports
Add static imports for Math.max and Math.min , then simplify the body to drop the Math. prefix.
🧩 Mini-Challenge — Wire up a multi-package project
Write the package and import lines for a class that lives in one package and uses types from two others.
You now understand how packages namespace and organise classes, how the package name maps to the folder path, and the full toolbox of imports — single, wildcard, static, and fully qualified names — plus why java.lang needs none. That's the skeleton every real Java project hangs on.
Next up: Dates & Times — working with LocalDate and LocalDateTime from the modern java.time API.
Practice quiz
Where must the 'package' statement appear in a Java source file?
- Anywhere in the file
- After the imports
- As the first line of code (comments above it are fine)
- Inside the class body
Answer: As the first line of code (comments above it are fine). The package declaration must be the first line of code, before any imports, and there can be only one.
How does the package name relate to the folder path?
- com.shop.model maps to the folder com/shop/model/
- They are unrelated
- The folder is always 'src'
- Only the last segment matters
Answer: com.shop.model maps to the folder com/shop/model/. The package name maps exactly to the directory path: com.shop.model means the file lives in com/shop/model/.
What does an import statement actually let you do?
- Copy the class into your file
- Speed up the program at runtime
- Declare your own package
- Refer to a class from another package by its short name
Answer: Refer to a class from another package by its short name. An import is a shortcut so you can use a class's simple name instead of its fully qualified name.
Which package is imported automatically into every Java file?
- java.util
- java.lang
- java.io
- java.time
Answer: java.lang. java.lang is auto-imported, which is why String, System, Math, and Integer need no import.
Which of these needs an explicit import?
- Scanner
- String
- Math
- System
Answer: Scanner. Scanner lives in java.util and needs an import. String, Math, and System are in java.lang.
What does 'import static java.lang.Math.PI;' allow?
- Importing the whole Math class
- Making PI mutable
- Writing PI instead of Math.PI
- Importing every static member of Math
Answer: Writing PI instead of Math.PI. A static import brings in a class's static members so you can use them without the class prefix, e.g. PI or sqrt(x).
What is true about a wildcard import like 'import java.util.*;'?
- It slows the program at runtime
- It has no runtime cost; the compiler only links classes you use
- It also imports java.util.concurrent
- It imports your own package
Answer: It has no runtime cost; the compiler only links classes you use. A wildcard has zero runtime cost. It does NOT reach sub-packages, and it can cause ambiguity for clashing simple names.
When is a fully qualified name (e.g. java.util.Date) most useful?
- Always, instead of imports
- Only in the main method
- Never — it is a syntax error
- When two classes share a simple name and you can only import one
Answer: When two classes share a simple name and you can only import one. When two classes (like java.util.Date and java.sql.Date) clash, you import one and use the other's full name inline.
What is the correct order of the top lines of a source file?
- imports, then package, then class
- package, then imports, then class
- class, then package, then imports
- package and imports can be in any order
Answer: package, then imports, then class. It is strictly: (optional comments), package, imports, then the class. Putting an import above package is a compile error.
Does 'import java.util.*;' include classes from java.util.concurrent?
- Yes, it includes all sub-packages
- Only at runtime
- No, a wildcard does not reach sub-packages
- Only if you also import java.lang
Answer: No, a wildcard does not reach sub-packages. A wildcard covers only the named package, not sub-packages. You must import java.util.concurrent separately.