Package: Naming, Type Imports

A package is Java's labelled shelf for classes: the package statement must be the first statement, folders mirror the dotted name, and import saves you from writing full addresses.

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Twenty-three files in one folder

BookBridge has grown: Book, Member, Librarian, FineReceipt, IssueRegister, OverdueException, Reminder... 23 files in one folder, and you just tried to add a second class called Report next to a teammate's Report.

A library solved this problem centuries ago: shelves with labels. Fiction here, reference there, same-titled books living peacefully on different shelves.

Java's labelled shelf is the package: a named group of related classes and interfaces.

Theory

Declaring the shelf

One line at the top of the file puts a class on a shelf:

package circulation;

public class IssueRegister { ... }

Rules the exam checks:

  • the package statement must be the first non-comment statement in the file, before any import or class
  • the folder structure must mirror the name: circulation's classes live in a circulation/ folder; package com.bookbridge.util lives in com/bookbridge/util/
  • convention: package names are lowercase, often a reversed domain (com.bookbridge) to stay globally unique

Practical

Two files, two shelves

// file: circulation/IssueRegister.java
package circulation;              // first statement, folder matches

public class IssueRegister {
    public void note(String title) {
        System.out.println("Issued: " + title);
    }
}

// file: BookBridge.java (default package, for the demo)
import circulation.IssueRegister; // single-type import

public class BookBridge {
    public static void main(String[] args) {
        IssueRegister r = new IssueRegister();
        r.note("Let Us C");
    }
}

Theory

Three ways to reach a shelved class

Once IssueRegister lives in circulation, other code has 3 access routes:

  • Single-type import: import circulation.IssueRegister; then use the short name. Precise, self-documenting, preferred.
  • On-demand import: import circulation.*; makes ALL of that package's classes available. Convenient, but hides where names come from.
  • Fully qualified name, no import at all: circulation.IssueRegister r = new circulation.IssueRegister(); Verbose; useful when 2 packages both offer a Report and you need both.

Quiz

import java.util.*; is at the top of a file. Which statement is TRUE?

  1. All classes of java.util AND its subpackages like java.util.regex are imported
  2. Only the classes directly inside java.util are imported; subpackages are not
  3. The entire class library is now imported, so the program compiles slower at run time
  4. It is a compile error; the star form does not exist
Show the answer

Only the classes directly inside java.util are imported; subpackages are not

The star reaches exactly one level: the classes of java.util itself. Subpackages are separate packages; java.util.regex needs its own import line. That is the trap option A walks into, and it is asked constantly. Option C doubles a myth: imports are a compile-time naming convenience and add no run-time cost. Option D denies a form you have already used. Remember: star means this shelf's books, not the whole wing.

Think first

The import you never wrote

String, System, Math: you have used them since lesson 1 with no import statement anywhere. Before tapping: why do they work?

Show the answer

They live in java.lang, the one package Java imports automatically into every file, precisely because no program survives without it. Everything else needs asking: Scanner sits in java.util, so keyboard input starts with import java.util.Scanner;. Exam one-liner: java.lang is imported by default; all other packages require an explicit import or a fully qualified name.

Watch out

Where package files break

Misplaced package statement: any statement before package circulation; (even an import) is a compile error. Order forever: package, then imports, then the class.

Folder mismatch: declaring package circulation; while the file sits elsewhere compiles, then dies at run time with NoClassDefFoundError; the JVM looks for circulation/IssueRegister.class and finds nothing. Compile with javac -d . and the folder tree is built for you.

Theory

You have been importing all semester

Nothing here is foreign: java.util, java.io and friends are just Sun's shelves, named by the reverse-domain convention with the domain shortened away. BookBridge now mirrors the pros: circulation for the desk classes, reports for month-end. The next lesson asks the natural follow-up: what does shelving do to ACCESS? That default no-modifier level from Unit 2, package-private, is about to finally show its purpose.

Summary

Key takeaways

  • A package is a named group of related classes; declare membership with package name; as the file's first statement.
  • Folders mirror the dotted name: com.bookbridge.util lives in com/bookbridge/util/.
  • Names are lowercase, conventionally reverse-domain for uniqueness.
  • Three access routes: single-type import, on-demand import (package.*), or fully qualified names.
  • The star imports one package's classes, never its subpackages.
  • java.lang alone is auto-imported: String and System work import-free.
  • Memory hook: package labels the shelf, import saves you reading the full address aloud.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Threads and Packages

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Package: Naming, Type Imports · Java Programming Language · Gri-Learn