Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Lesson 9: Packages, import, Classpath & Access Modifiers

Lesson 10 of 18 in the free Programming in Java notes on Siksha Sarovar, written by Rohit Jangra.

9.1 Why Packages?

A package is a namespace that groups related types, preventing name clashes (two libraries can each have a Logger), enabling access control (package-private visibility), and mapping onto the directory structure. Convention: reverse domain name — com.university.exams.

package com.university.exams;      // MUST be the first non-comment statement

public class ResultCalculator { /* ... */ }

Directory rule: the file must live at com/university/exams/ResultCalculator.java relative to a classpath root. Compile with javac -d out src/com/university/exams/ResultCalculator.java (the -d flag creates the package directories under out), then run with the fully qualified name: java -cp out com.university.exams.ResultCalculator.

9.2 import — Sugar, Not Loading

import merely lets you omit the package prefix; it copies nothing and loads nothing at runtime.

  • import java.util.List; — single type (preferred).
  • import java.util.; — on-demand; does not include subpackages (java.util.java.util.stream.*).
  • import static java.lang.Math.PI;static import of members: write sqrt(PI) instead of Math.sqrt(Math.PI).
  • java.lang is imported automatically — that's why String, System, Math need no import.
  • Name clash (java.util.Date vs java.sql.Date)? Import one, fully qualify the other.

Classpath: the list of roots (directories/JARs) where the JVM's class loader searches for .class files — set via -cp/-classpath or the CLASSPATH variable; default is the current directory. NoClassDefFoundError/ClassNotFoundException at launch is almost always a classpath mistake.

9.3 The Four Access Modifiers — Master Matrix

Access levelSame classSame packageSubclass (other pkg)World
private
default (no keyword)
protected
public

Memorize this matrix — it is asked verbatim, usually for 5 marks.

Fine print that earns the last mark:

  • Top-level classes may only be public or default — never private/protected.
  • Only one public top-level class per file, and the filename must match it.
  • protected across packages works only through inheritance — a subclass in another package can access the member on itself/its own type, not on an arbitrary parent instance.
  • Overriding may widen access (protected → public) but never narrow it (Lesson 6).

9.4 Encapsulation — The Modifiers' Purpose

Encapsulation = private state + controlled access through methods. It buys you validation, read-only or write-only views, and freedom to change the internal representation without breaking callers.

public class Student {
    private int marks;                          // hidden state
    public int getMarks() { return marks; }     // read access
    public void setMarks(int m) {               // guarded write access
        if (m < 0 || m > 100) throw new IllegalArgumentException("0-100 only");
        this.marks = m;
    }
}

A class with getters only is immutable-ish (read-only); the fully immutable recipe: final class, private final fields, no setters, defensive copies of mutable fields — exactly how String is built.

9.5 JARs & the Standard Library Layout

A JAR (Java ARchive) is a zipped tree of packages + a manifest: create with jar cfe app.jar com.uni.Main -C out ., run with java -jar app.jar. Know the flagship packages: java.lang (core, auto-imported), java.util (collections), java.io/java.nio (I/O), java.sql (JDBC), javax.swing/java.awt (GUI), java.net (networking).

🎯 Exam Focus

  1. What is a package? Explain how to create, compile (javac -d) and run a program in a user-defined package, with the directory structure.
  2. Reproduce the access-modifier matrix for all four levels against: same class, same package, subclass in another package, and the world.
  3. Explain protected's special behaviour across packages with a code example that compiles and one that does not.
  4. Differentiate import and import static. Does import java.util.* import java.util.stream? Why or why not?
  5. Define encapsulation. Write an immutable class Account and list the four ingredients of immutability.
  6. What are CLASSPATH and JAR files? How does the class loader locate com.uni.Main?