Skip to lesson
MentorNxt home

Java Programming

Packages, Imports & Access Control

Common mistakes and practice

A review of this module. Skip it if you feel confident; the next module does not need it.

Fix the mistakes

The slips that cost marks in this module. Spot each one, then check the fix.

Most package errors come from four slips: a wrong package line, a wrong run command, a missing import, or using something that is not public. Each one has a clear javac or java message.

Package line and run mistakes

A file will not compile or run, and the message points at the package line or the main class. These errors come from the order of lines, a missing semicolon, the file name or the run command.

Example 1 · An import above the package line

Topper.javaJava
// File: src/com/college/students/Topper.javaimport java.util.Arrays;package com.college.students;
public class Topper {    public static void main(String[] args) {        int[] marks = {72, 95, 60};        Arrays.sort(marks);        System.out.println("Topper scored " + marks[2]);    }}
Sample compiler output
src/com/college/students/Topper.java:3: error: class, interface, enum, or record expected
package com.college.students;
^
1 error

What this code does

  • Only comments may come before the package line. Here an import comes first, so javac rejects line 3.
  • Fix: swap lines 2 and 3. The program then prints Topper scored 95.
  • No semicolon after the package name → ';' expected.
  • Public class Topper saved in Top.java → class Topper is public, should be declared in a file named Topper.java.
  • java Welcome for a class in a package → Could not find or load main class Welcome; run java -cp out + the full name.

Review: Create your own package (opens in a new tab)

Import mistakes

javac says cannot find symbol or package … does not exist on a class name. Almost always the import is missing, misspelt, or does not reach the right package.

Java
import java.util.scanner;    // cannot find symbol: it is Scannerimport com.college.fess.*;   // package com.college.fess does not existimport com.college.*;        // does not reach com.college.fees

Two imports of Report clash; import one and write the full name for the other.

Review: Use classes from other packages with import (opens in a new tab)

Access mistakes

javac says has private access or is not public. Making everything public silences it, but then any class can change your data without checks. Fix the design instead: keep data private and add the one public method that is needed.

Example 2 · A class without public

Main.javaJava
// File: src/com/college/fees/Discount.javapackage com.college.fees;
class Discount {    public static int apply(int fee) {        return fee - 5000;    }}
// File: src/com/college/app/Main.javapackage com.college.app;
import com.college.fees.Discount;
public class Main {    public static void main(String[] args) {        System.out.println("Fee after discount: Rs " + Discount.apply(45000));    }}
Sample compiler output
src/com/college/app/Main.java:4: error: Discount is not public in com.college.fees; cannot be accessed from outside package
import com.college.fees.Discount;
                       ^
src/com/college/app/Main.java:8: error: Discount is not public in com.college.fees; cannot be accessed from outside package
        System.out.println("Fee after discount: Rs " + Discount.apply(45000));
                                                       ^
2 errors

What this code does

  • class Discount has no modifier, so the whole class has default access and is hidden outside com.college.fees.
  • Its method is public, but that does not help: other packages cannot see the class at all.

Example 3 · The fixed version

Discount.javaJava
// File: src/com/college/fees/Discount.javapackage com.college.fees;
public class Discount {    public static int apply(int fee) {        return fee - 5000;    }}
// File: src/com/college/app/Main.javapackage com.college.app;
import com.college.fees.Discount;
public class Main {    public static void main(String[] args) {        System.out.println("Fee after discount: Rs " + Discount.apply(45000));    }}
Sample output
Fee after discount: Rs 40000

What this code does

  • public class Discount lets Main import the class and call its public method.
  • Make a class public only when another package needs it; helpers can stay default.

The other two access errors: balance has private access in Wallet (a private member used from another class) and lateFine is not public in FeeCalculator (a default member used from another package).

Review: Access modifiers (opens in a new tab)

Spot the bug

Each code below breaks one rule from this module. Read what it should do, tap the wrong line, then check the fix.

Find the bug

Find the package or access bug

Select the line that is wrong.

This program reads a roll number with Scanner.

Round 1 of 5

Finish up

Mark complete & continue

Next: Build a simple calculator