Import Statements in Java

Naveen AnamaneniUpdated · 4 min read

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

Imports.java▶ Try it
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());
    }
}
Output
Navika, Srihitha, Advitha
Republic Day 2026: MONDAY

How it works

  1. import java.util.ArrayList; and import java.util.List; are single-type imports: each brings in one class.
  2. String needs no import: everything in java.lang is imported automatically.
  3. java.time.LocalDate is 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

FormExampleImports
Single-typeimport 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:

Dates.java
import java.util.*;
import java.sql.*;
 
public class Dates {
    Date today;
}
Compiler output
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 error

Two fixes:

  1. Add a single-type import, import java.util.Date;. A single-type import beats wildcards.
  2. Write the full name where you need the other one: java.sql.Date sqlDate.

What import Does Not Do

MythReality
It copies the class into your fileNo, it only shortens names for the compiler
It loads classes at startupNo, classes load when first used at runtime
Wildcards make programs slower or biggerNo, the bytecode is identical
It's needed to use a classNo, the full name always works without it
It works like C's #includeNo, 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

  1. Easy: Write a program that uses Scanner, ArrayList and LocalDate, with one single-type import for each.
  2. Harder: Write a class that needs both java.util.Date and java.sql.Date. Import one and use the full name for the other.

Quick check: Imports

4 questions
1Which package is imported automatically?
2Does import java.util.*; import java.util.function.Function?
3java.util.* and java.sql.* are both imported. What does Date d; do?
4Do wildcard imports slow programs down?

Key Takeaways

Was this tutorial helpful?

Lesson 147 of 482