Skip to lesson
MentorNxt home

Java Programming

Packages, Imports & Access Control

Use classes from other packages with import

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

TopMarks.javaJava
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));    }}
Output
[60, 72, 95]
Highest: 95
Root of 81: 9.0

What this code does

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.

Java
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.

A static import brings in one static method: after 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

FeeCalculator.javaJava
// 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);    }}
Sample output
Total fee: Rs 75000

What this code does

  • Main and FeeCalculator are in different packages, so Main needs the import line.
  • The order in Main.java is fixed: package line, import lines, then the class.
  • Compile both files with javac -d out …, then run java -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.

Java meets a short class name and looks in three places, in order: this package, then java.lang, then your import lines. If it is found, Java uses it. If it is in none of them, javac stops with cannot find symbol.

Example 3 · Forgetting the import

Main.javaJava
// 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);    }}
Sample compiler output
src/com/college/app/Main.java:6: error: cannot find symbol
        int fee = FeeCalculator.total(45000, 30000);
                  ^
  symbol:   variable FeeCalculator
  location: class Main
1 error

What this code does

  • Java looks for FeeCalculator in com.college.app and in java.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

Report.javaJava
// 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();    }}
Sample output
Student report: 32 students
Fee report: 30 paid, 2 pending

What this code does

  • Only the students Report is imported, so the short name Report means 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