Learn Java

Java import Statement, Explained with Examples

By Updated 8 min read

In Java, an import statement at the top of a source file lets you use a class from another package by its short name, such as Scanner instead of java.util.Scanner. It only tells the compiler which class a name means: it copies no code into your file and costs nothing when the program runs. Below you can run an example of each kind of import, see which classes need no import at all, and fix the errors a wrong import causes.

Main.java
1import java.util.Scanner;
2
3public class Main {
4 public static void main(String[] args) {
5 Scanner in = new Scanner(System.in);
6 System.out.print("What is your name? ");
7 String name = in.nextLine();
8 System.out.println("Hello, " + name + "!");
9 }
10}
Prints
›What is your name? Ada
›Hello, Ada!

Press Run and it greets whatever name is in the input box: Hello, Ada!. Scanner, the class that reads the keyboard, lives in a package, a named group of related classes, called java.util. The first line says that in this file, Scanner means java.util.Scanner.

How to import a class in Java#

An import line is the word import, the class's full name, and a semicolon. The full name, called the fully qualified name, is the package, a dot and the class: java.util.Scanner. You can always skip the import and write the full name wherever you use the class. java.util.Scanner in = new java.util.Scanner(System.in); compiles to the same program as the example above.

That is all an import does. It copies no code into your file and loads nothing while the program runs, and the compiled .class file names every class in full anyway. So an import has no cost at run time, and an unused one does no harm beyond clutter.

Import lines go at the top of the file: after the package line if the file has one, and before the class. Their order among themselves does not matter.

Key idea

An import is a shortcut for the compiler. It lets you write Scanner where you mean java.util.Scanner, and nothing more.

Common imports: Scanner, ArrayList, File and more#

These are the imports a beginner writes most. Each one names the package the class lives in, and the documentation page of every class shows its package near the top.

To useWrite this importUsed for
Scannerimport java.util.Scanner;Reading keyboard input
ArrayList, Listimport java.util.ArrayList; and import java.util.List;Lists that grow
HashMap, Mapimport java.util.HashMap; and import java.util.Map;Looking values up by a key
Arrays, Randomimport java.util.Arrays; and import java.util.Random;Array helpers, random numbers
Functionimport java.util.function.Function;Lambdas that take and return a value
LocalDateimport java.time.LocalDate;Dates
File, IOExceptionimport java.io.File; and import java.io.IOException;A file on disk, and the error that file operations can throw
Files, Pathimport java.nio.file.Files; and import java.nio.file.Path;Reading and writing files
BigDecimalimport java.math.BigDecimal;Exact decimals, such as money
String, Math, System, IntegerNo importThey are in java.lang, imported for you
One import line per class. The star form in the next section covers a whole package at once.

For whole programs that work with files, see how to read a file and how to write to a file.

What does import java.util.* mean?#

A star in place of the class name imports a whole package: import java.util.*; lets you use every public class and interface in java.util by its short name. The compiler looks in the package only when it meets a name it does not otherwise know, so this is called an on-demand import.

Main.java
1import java.util.*;
2
3public class Main {
4 public static void main(String[] args) {
5 List<String> names = new ArrayList<>(List.of("Grace", "Ada", "Linus"));
6 Collections.sort(names);
7
8 Map<String, Integer> ages = new HashMap<>();
9 ages.put("Ada", 36);
10
11 System.out.println(names);
12 System.out.println(ages);
13 }
14}
Prints
›[Ada, Grace, Linus]
›{Ada=36}

It prints [Ada, Grace, Linus] and {Ada=36}. One line covers List, ArrayList, Collections, Map and HashMap. The star does not make the program bigger or slower: it does the same work and loads the same classes as a version with five single imports.

A star does not reach into other packages
Package names look nested, as in java.util and java.util.function, but Java treats them as two unrelated packages. So import java.util.*; does not import Function, and using it anyway makes javac report cannot find symbol with symbol: class Function. Add import java.util.function.Function;.

Why not use import * in Java?#

A star import works, and in a small program it does no harm. Many teams still prefer one import per class, for two reasons. The list at the top then tells a reader exactly which classes the file uses and where each comes from. And it avoids name clashes: two packages can each have a class with the same name, and with two star imports javac cannot tell which one you mean. java.awt, Java's older toolkit for windows and buttons, has a List class of its own, an on-screen list box:

Main.java (does not compile)
read along
1import java.awt.*;
2import java.util.*;
3
4public class Main {
5 public static void main(String[] args) {
6 List<String> names = new ArrayList<>();
7 names.add("Ada");
8 System.out.println(names);
9 }
10}
Terminal
read along
1$ javac Main.java
2Main.java:6: error: reference to List is ambiguous
3 List<String> names = new ArrayList<>();
4 ^
5 both interface java.util.List in java.util and class java.awt.List in java.awt match
61 error

Either fix works. Add import java.util.List;, since an import that names one class beats any star import, or write the full name, java.util.List<String> names. Then it compiles and prints [Ada]. java.util.Date and java.sql.Date clash the same way.

A clash can even appear in code nobody touched. java.awt.List dates from Java 1.0 and java.util.List from Java 1.2, so a file written for Java 1.1 that imported both packages with stars and used List no longer compiled with Java 1.2.

Let the editor write imports
Java editors add the import line when you first use a class, and can remove the ones you no longer need, so one import per class costs no typing.

Classes that need no import#

Two kinds of class never need an import. The first is everything in java.lang: Java imports that package into every file for you, as if each file began with import java.lang.*;. That is why String, Math, System and Integer work in your very first program. The second is every class in the same package as your file, since classes in one package always see each other by their short names.

Main.java
1public class Main {
2 public static void main(String[] args) {
3 String city = "Lisbon";
4 System.out.println(city.toUpperCase());
5 System.out.println(Math.max(3, 7));
6 System.out.println(Integer.parseInt("42") + 1);
7
8 Product tea = new Product("Green tea", 3.5);
9 System.out.println(tea.label());
10 }
11}
12
13class Product {
14 String name;
15 double price;
16
17 Product(String name, double price) {
18 this.name = name;
19 this.price = price;
20 }
21
22 String label() {
23 return name + ": " + price;
24 }
25}
Prints
›LISBON
›7
›43
›Green tea: 3.5

It has no import lines, and it prints LISBON, 7, 43 and Green tea: 3.5. String, Math, System and Integer come from java.lang. Product is declared in the same file, so it is in the same package as Main. A file with no package line puts its classes in the unnamed package, which is where every runnable program on this page lives.

How to import your own class from another package#

You import a class, never a file. To use one of your own classes from another package, three things must line up: the class's file starts with a package line, the file sits in folders that match the package name, and the class is public. Here a Product lives in the package com.example.shop, and the Main that uses it in com.example.app:

project layout
read along
1src/
2 com/
3 example/
4 app/
5 Main.java (package com.example.app;)
6 shop/
7 Product.java (package com.example.shop;)
src/com/example/shop/Product.java
read along
1package com.example.shop;
2
3public class Product {
4 private final String name;
5 private final double price;
6
7 public Product(String name, double price) {
8 this.name = name;
9 this.price = price;
10 }
11
12 public String label() {
13 return name + ": " + price;
14 }
15}
src/com/example/app/Main.java
read along
1package com.example.app;
2
3import com.example.shop.Product;
4
5public class Main {
6 public static void main(String[] args) {
7 Product tea = new Product("Green tea", 3.5);
8 System.out.println(tea.label());
9 }
10}
Terminal
read along
1$ javac -d out src/com/example/shop/Product.java src/com/example/app/Main.java
2$ java -cp out com.example.app.Main
3Green tea: 3.5

Run both commands from the folder that holds src. -d out puts the compiled classes in a folder called out, inside package folders of their own, and -cp out tells java to look for classes there. java takes the full name of the class to start, com.example.app.Main. The top of Main.java also shows the order every file follows: the package line, then the imports, then the class.

A class without public cannot be imported
Leave out public on Product and javac stops at the import line with Product is not public in com.example.shop; cannot be accessed from outside package. A class without public can be used only inside its own package.

Classes from a library are imported the same way, for example import org.junit.jupiter.api.Test;. The library's JAR file must also be on the classpath, the list of places Java searches for classes, which build tools such as Maven and Gradle arrange for you.

Can you import a single method? Static imports#

Only a static one. An ordinary import names a class, or a whole package with a star, so import java.lang.Math.max; fails: javac reads max as a class name and reports cannot find symbol with symbol: class max. A static importbrings in a class's static members, its static methods and constants, so you can use them without the class name in front:

Main.java
1import static java.lang.Math.PI;
2import static java.lang.Math.sqrt;
3
4public class Main {
5 public static void main(String[] args) {
6 double radius = 2.0;
7 System.out.printf("Area of the circle: %.2f%n", PI * radius * radius);
8 System.out.println("Side of the square: " + sqrt(49));
9 }
10}
Prints
›Area of the circle: 12.57
›Side of the square: 7.0

It prints Area of the circle: 12.57 and Side of the square: 7.0. import static java.lang.Math.*; would bring in every static member of Math at once. Math itself needs no import, since it is in java.lang: the static imports only save you writing Math. in front. Instance methods, such as a string's toUpperCase, can never be imported, because they need an object to run on.

Static imports pay off for a few members a file uses again and again, such as assertEquals in a test class or PI in geometry code. Used widely, they hide where a name comes from: a reader who meets max(72, 91) has to scroll up to learn that it is Math.max. Oracle's tutorial advises using them sparingly, and the static and final lesson explains what static members are.

Fix the cannot find symbol error#

When a class is not imported, javac does not know what its short name means. Here is its answer to a file that uses Scanner with no import line:

Terminal
read along
1$ javac Main.java
2Main.java:3: error: cannot find symbol
3 Scanner in = new Scanner(System.in);
4 ^
5 symbol: class Scanner
6 location: class Main
7Main.java:3: error: cannot find symbol
8 Scanner in = new Scanner(System.in);
9 ^
10 symbol: class Scanner
11 location: class Main
122 errors

javac complains once for each Scanner on the line. The usual causes:

  • The import is missing. Add import java.util.Scanner; at the top.
  • The name is misspelled. Java is case-sensitive, so scanner and Scanner are different names.
  • The import names the wrong package. import java.util.Function; fails with cannot find symbol, because Function is in java.util.function, and a typo such as import java.utils.Scanner; gives package java.utils does not exist.
  • For your own class in another package, the package line and the folders disagree, or the class is not public.
The Run button does not check imports
The engine on this page runs a program even when an import line is missing: delete the first line of the opening example and it still runs. Real javac does check, so on your own computer every import must be there.

Import a whole module: import module java.base#

Java 25 adds a wider import still. A moduleis a named group of packages, and the core of Java's library is the module java.base, which holds java.lang, java.util, java.util.function, java.io, java.time and dozens more. The line import module java.base; imports every public class of every package that module exports. It became a standard part of Java in Java 25 (JEP 511).

Main.java
1import module java.base;
2
3public class Main {
4 public static void main(String[] args) {
5 List<String> names = new ArrayList<>(List.of("Grace", "Ada"));
6 Collections.sort(names);
7 LocalDate release = LocalDate.of(2025, 9, 16);
8 Function<String, Integer> length = String::length;
9
10 System.out.println(names);
11 System.out.println(release.getDayOfWeek());
12 System.out.println(length.apply("module"));
13 }
14}
Prints
›[Ada, Grace]
›TUESDAY
›6

It prints [Ada, Grace], TUESDAY (the day of the week Java 25 was released) and 6, with classes from four packages and one import line. A compact source file, one that starts with void main() and has no class written around it, gets this import automatically (JEP 512), so it can use List or Scanner with no import line at all. In bigger programs most teams still list their classes one by one, and an import that names one class settles a clash here just as it does with stars. The packages and imports lesson covers packages, the classpath and JAR files in full.

Questions people ask#

What does import do in Java?
An import lets you use a class from another package by its short name, such as Scanner for java.util.Scanner. It only tells the compiler which class a name means, so it copies no code and costs nothing when the program runs.
What does import java.util.* mean?
It lets you use every public class and interface in the java.util package by its short name. It does not include packages whose names merely start the same way, such as java.util.function, which need imports of their own.
Why do people avoid import * in Java?
A star import hides which package each class comes from, and two star imports that both contain a class with the same name, such as List in java.util and in java.awt, make that name ambiguous. It does not make a program bigger or slower.
How do I import a class from another Java file?
You import the class, not the file. If the class is in the same package, no import is needed. If it is in another package, it must be public, and you write its package and class name, such as import com.example.shop.Product;.
Can you import a single method in Java?
Only a static method, with a static import such as import static java.lang.Math.max;. An ordinary import names a class, or a whole package with a star, and instance methods cannot be imported at all.
Which Java classes do not need an import?
The classes in java.lang, such as String, Math, System and Integer, are imported into every file automatically. Classes in the same package as your file need no import either.

Further reading#

Learn it step by step

These lessons teach the topic from the start, with more examples and questions to check yourself.

All guides

Spotted a mistake, or a part that did not make sense? Report it by email