Use classes from other packages with import
22 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)An import line lets you use a class from another package by its short name. Without it, you must write the full name, like java.util.Arrays, every time.
import: use the short name
Writing java.util.Arrays every time you sort is long. An import line, written once at the top of the file, lets you use the short name Arrays instead.
Example 1 · import instead of the full name
import java.util.Arrays;
public class TopMarks { public static void main(String[] args) { int[] marks = {72, 95, 60}; Arrays.sort(marks); System.out.println(Arrays.toString(marks)); System.out.println("Highest: " + marks[marks.length - 1]); System.out.println("Root of 81: " + Math.sqrt(81)); }}[60, 72, 95] Highest: 95 Root of 81: 9.0
What this code does
- After
import java.util.Arrays;the short nameArraysis enough, in every line of the file. Mathneeds no import: it is injava.lang, as in Why do we need packages? (opens in a new tab).
Import lines go after the package line and before the class. An import only tells javac where to find a class: it copies no code and does not make the program bigger.
One class or the whole package
A program needs both Scanner and Arrays from java.util. A single-class import names one class; a wildcard import uses * to bring in every class of one package.
import java.util.Scanner; // only Scannerimport java.util.*; // every class in java.util* covers one package only, not the packages inside it: import com.college.*; does not give you com.college.fees.FeeCalculator. Many teams prefer single-class imports, because they show exactly which classes a file uses.
import static java.lang.Math.max; you can write max(4, 9). Use it rarely, so readers still see where a method comes from.Importing your own class
Main is in com.college.app, but it needs FeeCalculator from com.college.fees. You import your own classes exactly like Java's: package name, a dot, class name.
Example 2 · Importing your own class
// File: src/com/college/fees/FeeCalculator.javapackage com.college.fees;
public class FeeCalculator { public static int total(int tuition, int hostel) { return tuition + hostel; }}
// File: src/com/college/app/Main.javapackage com.college.app;
import com.college.fees.FeeCalculator;
public class Main { public static void main(String[] args) { int fee = FeeCalculator.total(45000, 30000); System.out.println("Total fee: Rs " + fee); }}Total fee: Rs 75000
What this code does
MainandFeeCalculatorare in different packages, soMainneeds the import line.- The order in
Main.javais fixed: package line, import lines, then the class. - Compile both files with
javac -d out …, then runjava -cp out com.college.app.Main.
Where Java looks for a short name
You forget the import, and javac says cannot find symbol. For a short class name, Java looks in three places: the class's own package, java.lang, and the import lines.
Example 3 · Forgetting the import
// File: src/com/college/app/Main.javapackage com.college.app;
public class Main { public static void main(String[] args) { int fee = FeeCalculator.total(45000, 30000); System.out.println("Total fee: Rs " + fee); }}src/com/college/app/Main.java:6: error: cannot find symbol
int fee = FeeCalculator.total(45000, 30000);
^
symbol: variable FeeCalculator
location: class Main
1 errorWhat this code does
- Java looks for
FeeCalculatorincom.college.appand injava.lang, and there are no import lines, so it is not found. - Fix: add
import com.college.fees.FeeCalculator;below the package line.
Classify
Does it need an import?
The code is in class Main, package com.college.app. Tap a name, then tap the right box.
Two classes with the same name
One file needs both com.college.students.Report and com.college.fees.Report. Import both, and javac stops with a type with the same simple name is already defined. The fix: import one, and write the full name for the other.
Example 4 · Two classes named Report
// File: src/com/college/students/Report.javapackage com.college.students;
public class Report { public static void print() { System.out.println("Student report: 32 students"); }}
// File: src/com/college/fees/Report.javapackage com.college.fees;
public class Report { public static void print() { System.out.println("Fee report: 30 paid, 2 pending"); }}
// File: src/com/college/app/Reports.javapackage com.college.app;
import com.college.students.Report;
public class Reports { public static void main(String[] args) { Report.print(); com.college.fees.Report.print(); }}Student report: 32 students Fee report: 30 paid, 2 pending
What this code does
- Only the students
Reportis imported, so the short nameReportmeans that one. com.college.fees.Report.print()uses the full name, so Java knows it is the other one. Java has no way to rename a class in an import.
Next step
Check your understanding
9 quick questions