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.
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("What is your name? "); String name = in.nextLine(); System.out.println("Hello, " + name + "!"); }}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 ideaAn import is a shortcut for the compiler. It lets you write
Scannerwhere you meanjava.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 use | Write this import | Used for |
|---|---|---|
Scanner | import java.util.Scanner; | Reading keyboard input |
ArrayList, List | import java.util.ArrayList; and import java.util.List; | Lists that grow |
HashMap, Map | import java.util.HashMap; and import java.util.Map; | Looking values up by a key |
Arrays, Random | import java.util.Arrays; and import java.util.Random; | Array helpers, random numbers |
Function | import java.util.function.Function; | Lambdas that take and return a value |
LocalDate | import java.time.LocalDate; | Dates |
File, IOException | import java.io.File; and import java.io.IOException; | A file on disk, and the error that file operations can throw |
Files, Path | import java.nio.file.Files; and import java.nio.file.Path; | Reading and writing files |
BigDecimal | import java.math.BigDecimal; | Exact decimals, such as money |
String, Math, System, Integer | No import | They are in java.lang, imported for you |
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.
import java.util.*; public class Main { public static void main(String[] args) { List<String> names = new ArrayList<>(List.of("Grace", "Ada", "Linus")); Collections.sort(names); Map<String, Integer> ages = new HashMap<>(); ages.put("Ada", 36); System.out.println(names); System.out.println(ages); }}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.
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:
import java.awt.*;import java.util.*; public class Main { public static void main(String[] args) { List<String> names = new ArrayList<>(); names.add("Ada"); System.out.println(names); }}$ javac Main.javaMain.java:6: error: reference to List is ambiguous List<String> names = new ArrayList<>(); ^ both interface java.util.List in java.util and class java.awt.List in java.awt match1 errorEither 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.
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.
public class Main { public static void main(String[] args) { String city = "Lisbon"; System.out.println(city.toUpperCase()); System.out.println(Math.max(3, 7)); System.out.println(Integer.parseInt("42") + 1); Product tea = new Product("Green tea", 3.5); System.out.println(tea.label()); }} class Product { String name; double price; Product(String name, double price) { this.name = name; this.price = price; } String label() { return name + ": " + price; }}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:
src/ com/ example/ app/ Main.java (package com.example.app;) shop/ Product.java (package com.example.shop;)package com.example.shop; public class Product { private final String name; private final double price; public Product(String name, double price) { this.name = name; this.price = price; } public String label() { return name + ": " + price; }}package com.example.app; import com.example.shop.Product; public class Main { public static void main(String[] args) { Product tea = new Product("Green tea", 3.5); System.out.println(tea.label()); }}$ javac -d out src/com/example/shop/Product.java src/com/example/app/Main.java$ java -cp out com.example.app.MainGreen tea: 3.5Run 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.
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:
import static java.lang.Math.PI;import static java.lang.Math.sqrt; public class Main { public static void main(String[] args) { double radius = 2.0; System.out.printf("Area of the circle: %.2f%n", PI * radius * radius); System.out.println("Side of the square: " + sqrt(49)); }}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:
$ javac Main.javaMain.java:3: error: cannot find symbol Scanner in = new Scanner(System.in); ^ symbol: class Scanner location: class MainMain.java:3: error: cannot find symbol Scanner in = new Scanner(System.in); ^ symbol: class Scanner location: class Main2 errorsjavac 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
scannerandScannerare different names. - The import names the wrong package.
import java.util.Function;fails withcannot find symbol, becauseFunctionis injava.util.function, and a typo such asimport java.utils.Scanner;givespackage java.utils does not exist. - For your own class in another package, the
packageline and the folders disagree, or the class is notpublic.
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).
import module java.base; public class Main { public static void main(String[] args) { List<String> names = new ArrayList<>(List.of("Grace", "Ada")); Collections.sort(names); LocalDate release = LocalDate.of(2025, 9, 16); Function<String, Integer> length = String::length; System.out.println(names); System.out.println(release.getDayOfWeek()); System.out.println(length.apply("module")); }}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
Scannerforjava.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.utilpackage by its short name. It does not include packages whose names merely start the same way, such asjava.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
Listinjava.utiland injava.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 asimport 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 asString,Math,SystemandInteger, are imported into every file automatically. Classes in the same package as your file need no import either.