Import Statements in Java
Every class has a full name such as java.util.ArrayList. Writing that each time would be tiring, so an import statement at the top of the file lets you use the short name instead. An import is purely a convenience for the compiler: it doesn't copy code, load anything or slow your program down. It just tells the compiler, "when I write ArrayList, I mean java.util.ArrayList".
Three Ways to Refer to a Class
import java.util.ArrayList;
import java.util.List;
public class Imports {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Navika");
names.add("Srihitha");
names.add("Advitha");
System.out.println(String.join(", ", names));
java.time.LocalDate republicDay = java.time.LocalDate.of(2026, 1, 26);
System.out.println("Republic Day 2026: " + republicDay.getDayOfWeek());
}
}Navika, Srihitha, Advitha
Republic Day 2026: MONDAYHow it works
import java.util.ArrayList;andimport java.util.List;are single-type imports: each brings in one class.Stringneeds no import: everything injava.langis imported automatically.java.time.LocalDateis written with its fully qualified name and no import. That's handy when you use a class once, or when two classes share a name.
Try it yourself: replace the two imports with import java.util.*;. Does it still compile?
Single-Type vs Wildcard
| Form | Example | Imports |
|---|---|---|
| Single-type | import java.util.ArrayList; | Exactly one class |
| Type-import-on-demand (wildcard) | import java.util.*; | Any class from java.util that you actually use |
| Module import (Java 25) | import module java.base; | Every package a module exports (see "Module Import Declarations (Java 25)") |
A wildcard covers one package only. import java.util.*; does not import java.util.function.Function; sub-packages are separate packages.
Neither form affects speed or the size of the .class file: the compiler records only the classes you actually use. Most teams prefer single-type imports because they show exactly where each class comes from, and IDEs manage them automatically.
Name Clashes
Both java.util and java.sql have a class named Date. With two wildcards, a plain Date is ambiguous:
import java.util.*;
import java.sql.*;
public class Dates {
Date today;
}Dates.java:5: error: reference to Date is ambiguous
Date today;
^
both class java.sql.Date in java.sql and class java.util.Date in java.util match
1 errorTwo fixes:
- Add a single-type import,
import java.util.Date;. A single-type import beats wildcards. - Write the full name where you need the other one:
java.sql.Date sqlDate.
What import Does Not Do
| Myth | Reality |
|---|---|
| It copies the class into your file | No, it only shortens names for the compiler |
| It loads classes at startup | No, classes load when first used at runtime |
| Wildcards make programs slower or bigger | No, the bytecode is identical |
| It's needed to use a class | No, the full name always works without it |
It works like C's #include | No, there's no text inclusion at all |
Common Mistakes
Expecting import java.util.* to include sub-packages. java.util.function and java.util.concurrent need their own imports.
Importing a class from the unnamed (default) package. It's impossible; put the class in a named package.
Putting imports before the package line. The order is: package, then imports, then types.
Interview Corner
Does importing a package with a wildcard affect performance?
No. Imports are resolved at compile time; the compiler writes fully qualified references only for classes actually used, so the bytecode is the same with single-type or wildcard imports. Class loading at runtime depends on use, not on imports. Wildcards can only cause compile-time name ambiguities.
Do you need to import java.lang?
No. Every compilation unit implicitly imports java.lang.*, so String, Math, System, Integer and Object are available directly.
Practice
- Easy: Write a program that uses
Scanner,ArrayListandLocalDate, with one single-type import for each. - Harder: Write a class that needs both
java.util.Dateandjava.sql.Date. Import one and use the full name for the other.
Key Takeaways
Was this tutorial helpful?