Skip to main content

How to Import a Class in Java

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Java's classes live in packages: LocalDate lives in java.time, ArrayList in java.util. An import line at the top of a file lets you use a class by its short name, LocalDate, instead of its full one every time.

Syntax​

Syntax
import <package>.<ClassName>;
import <package>.*;
PartWhat it does
import <package>.<ClassName>;Makes one class usable by its simple name.
import <package>.*;Makes every class in the package usable by its simple name.
PositionAfter the package line, if there is one, and before the class.

The Java Language Specification, section 7.5: "An import declaration allows a named class, interface, or static member to be referred to by a simple name that consists of a single identifier."

The Example​

The example imports LocalDate from java.time and prints the day of the week of 26 November 2026.

Prerequisites​

  • A JDK, the Java Development Kit. java Main.java runs a single file without compiling it first. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Import​

Create a file named Main.java:

Main.java, which imports java.time.LocalDate and prints the day of the week of LocalDate.of(2026, 11, 26).

Main.java
import java.time.LocalDate;

public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2026, 11, 26);
System.out.println(date.getDayOfWeek());
}
}

Step 2: Run It​

Terminal
java Main.java
Output
THURSDAY

A zsh terminal where java Main.java prints THURSDAY.

The command is the same in zsh, bash and PowerShell.

What Happens Without It​

Delete the import line and the compiler can't find the class:

Output
Main.java:4: error: cannot find symbol
LocalDate date = LocalDate.of(2026, 11, 26);
^
symbol: class LocalDate
location: class Main
Main.java:4: error: cannot find symbol
LocalDate date = LocalDate.of(2026, 11, 26);
^
symbol: variable LocalDate
location: class Main
2 errors
error: compilation failed

Both uses of LocalDate on that line fail, and the program doesn't run.

Why String and System Need No import​

Everything in the java.lang package, which includes String, System and Integer, is imported into every file automatically. Classes from any other package, such as java.time or java.util, need an import.

Common Mistakes​

MistakeWhat happensDo this instead
Forgetting the importerror: cannot find symbol with symbol: class …Add the import for that class
Importing a package, as in import java.time;error: cannot find symbol: an import names a class, or ends in .*Write import java.time.LocalDate; or import java.time.*;
Importing two classes with the same simple name, such as java.util.Date and java.sql.Dateerror: a type with the same simple name is already defined by the single-type-import of DateImport one and write the other's full name

Troubleshooting​

SymptomLikely causeFix
cannot find symbol for a class you know existsIts package isn't importedLook up the class's package in its Javadoc and import it
error: package java.tiem does not existThe package name is misspelled, or it's a library that isn't on the class pathCheck the spelling, or add the library

FAQ​

Is import java.time.* slower than importing one class?​

No. An import only tells the compiler where to look up names; the compiled program is the same.

Can I skip the import?​

Yes, by writing the full name every time: java.time.LocalDate date = java.time.LocalDate.of(2026, 11, 26);.

Conclusion​

import package.ClassName; lets you use a class by its short name. java.lang is imported for you; everything else needs an import line at the top of the file.

Additional Resources​