Java Import Statement
Table of Contents + −
In the last lesson you learned about Java packages. But once a class lives inside a package, its real name gets long, like java.util.Scanner. Typing that every single time would be exhausting. The import statement is the fix. Let’s learn how it works, step by step.
🤔 Why do we need import?
The Scanner class lives at java.util.Scanner. Without an import, you spell out that whole address everywhere. Look how noisy it gets.
public class NoImport { public static void main(String[] args) { java.util.Scanner scanner = new java.util.Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.nextLine(); System.out.println("Hello, " + name); }}That long name is the fully qualified name: the package, then the class name. It works, but:
- Reading it twice on one line is painful.
- A real program mentions a class far more than twice.
importlets you say the long name once at the top, then useScannereverywhere.
Same program, much cleaner.
import java.util.Scanner;
public class WithImport { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.nextLine(); System.out.println("Hello, " + name); }}Output
Enter your name: AlexHello, AlexThat is the whole point of import. It tells Java “when I write Scanner, I mean java.util.Scanner”.
📦 Single-type import
The kind you just saw is a single-type import: import, then the package path, then one class. It is the clearest kind.
- Each line names exactly one class.
- A reader sees precisely which classes you pulled in, and from where.
- No guessing about what is in use.
Here is a small program that imports two classes and uses both.
import java.util.ArrayList;import java.util.Collections;
public class ScoreSort { public static void main(String[] args) { ArrayList<Integer> scores = new ArrayList<>(); scores.add(40); scores.add(10); scores.add(25);
Collections.sort(scores); System.out.println(scores); }}We imported ArrayList and Collections from java.util, then used their short names. Java knew each full address because we listed them at the top.
Output
[10, 25, 40]✳️ Wildcard import
A wildcard import brings in every class from a package at once, with the star symbol in place of a class name.
- One line makes every class in the package available by short name.
- It hides which classes you actually use.
- It raises the chance of a name clash.
import java.util.*;That one line makes Scanner, ArrayList, HashMap, Collections, and every other java.util class available by short name. The score program above would work with this single line instead of two named imports. Most teams still prefer single-type imports.
The star is not a folder wildcard
import java.util.* brings in the classes directly inside java.util. It does not reach into sub-packages. So java.util.* does not include java.util.regex.Pattern. Sub-packages need their own import line.
🌐 java.lang is automatic
You have used String, System, Math, Integer from the start without importing them. Why not?
- They live in java.lang, which holds the most essential everyday classes.
- Java imports that package for you automatically.
- It is as if every file secretly begins with
import java.lang.*.
public class NoImportNeeded { public static void main(String[] args) { String message = "Hello"; // String is from java.lang int length = message.length(); double root = Math.sqrt(144); // Math is from java.lang System.out.println(length + " " + root); }}We used String, Math, and System with no import at all. That is java.lang doing its quiet work in the background.
Output
5 12.0➗ Static import
A normal import brings in a class. A static import brings in the static members of a class, so you use them without the class name in front. Without it, you repeat Math. each time.
public class WithoutStatic { public static void main(String[] args) { double area = Math.PI * Math.pow(5, 2); double root = Math.sqrt(81); System.out.println(area + " " + root); }}With a static import, you can drop the Math. part and call sqrt, pow, and PI directly.
import static java.lang.Math.*;
public class WithStatic { public static void main(String[] args) { double area = PI * pow(5, 2); double root = sqrt(81); System.out.println(area + " " + root); }}Output
78.53981633974483 9.0The word static sits between import and the path. Use it with care:
- Handy for math-heavy code.
- A bare
sqrthides where it came from. A littleMath.in front helps people follow the code. - Reach for it only when it makes things clearer.
⚔️ Resolving name clashes
Java has two classes named Date: java.util.Date and java.sql.Date. Import both and Java cannot tell which you mean, so it refuses to compile. The fix: import one, then write the fully qualified name for the other.
import java.util.Date;
public class TwoDates { public static void main(String[] args) { Date now = new Date(); // this is java.util.Date java.sql.Date today = new java.sql.Date(now.getTime()); // full name for the other
System.out.println("Util date class: " + now.getClass().getName()); System.out.println("Sql date class: " + today.getClass().getName()); }}The short name Date means the imported one. The SQL version is spelled out in full. Only one short name is in play, so no clash.
Output
Util date class: java.util.DateSql date class: java.sql.DateWhen in doubt about which class you mean, write the full address.
💡 Import does not copy code
A common myth: import copies the class’s code in and makes the program bigger or slower. It does not.
- An import is a compile-time note: “this short name means this full name”.
- The compiler resolves your short names, then the import does nothing at runtime.
- Classes like
Scanneralready live in Java’s libraries. Your program links to them, it does not carry a copy. - Unused imports cost nothing in size or speed. They only clutter the file for the reader.
⚠️ Common Mistakes
- Wildcard ambiguity. Two wildcard imports can offer a class with the same name. Java cannot pick one, so the file will not compile.
// ❌ both packages have a class named "List", so "List" below is ambiguousimport java.util.*;import java.awt.*;List names; // compile error: which List?
// ✅ import the exact one you wantimport java.util.List;import java.awt.*;List names; // clear: this is java.util.List- Importing the wrong class. Importing
java.sql.Datewhen you wantedjava.util.Dategives you methods you did not expect. Check the package path, not just the class name.
import java.sql.Date; // ❌ probably not the calendar date you wantedimport java.util.Date; // ✅ the everyday date and time class- Thinking import copies code. An import only maps a short name to a full name. It adds nothing to the running program.
import java.util.Scanner; // ✅ this is a name shortcut, not a code copy✅ Best Practices
- Prefer single-type imports. List each class by name so readers see exactly what you use.
import java.util.Scanner; // ✅ clear and specificimport java.util.ArrayList;- Use wildcards sparingly. Reach for
java.util.*only when you pull in many classes from one package. - Use the fully qualified name to break a clash. Import one class, spell out the other in full.
- Use static imports only when they help. Keep
Math.in front unless the code is clearly better without it. - Remove unused imports. They clutter the top of the file and mislead the reader.
🧩 What You’ve Learned
- ✅ An import lets you use a class by its short name instead of its long fully qualified name.
- ✅ A single-type import brings in one class; a wildcard import brings in a whole package with the star symbol.
- ✅ Single-type imports are clearer; wildcards save typing but hide what you use and risk clashes.
- ✅ The java.lang package is imported automatically, which is why
String,Math, andSystemneed no import. - ✅ A static import lets you call static members like
sqrtandPIwithout the class name in front. - ✅ To fix a name clash, import one class and write the fully qualified name for the other.
- ✅ An import is a compile-time shortcut. It does not copy code or make your program bigger or slower.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does an import statement actually do?
Why: An import maps a short name to a fully qualified name at compile time. It does not copy code.
- 2
Why can you use String and Math without importing them?
Why: java.lang holds the most essential classes and Java imports it for you automatically.
- 3
Which line lets you write sqrt instead of Math.sqrt?
Why: A static import brings in static members so you can call them without the class name in front.
- 4
You need both java.util.Date and java.sql.Date. What is the cleanest fix?
Why: Two classes with the same short name clash, so import one and spell out the full name for the other.
🚀 What’s Next?
You now know how import and static import bring names into your file. The word static keeps showing up, so let’s slow down and really understand what it means and when to use it.