Skip to main content

Language Features

Packages

Complete lesson for Packages using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.

L1

Basics — Packages

Section: Language Features. package and import, java.lang, static imports, access across packages, and classpath layout.

What is a package?

  • A package is a namespace for types. The first line of the file is package com.learncodeguru.java; (no import before it except comments).
  • The folder path must match the package: com/learncodeguru/java/Hello.java. Compile and run from the *source root*.
  • Convention: reverse-DNS, all lowercase, no underscores if you can avoid them.
package demo.util;

public class Greetings {
    public static String hi(String name) {
        return "Hello, " + name;
    }

    public static void main(String[] args) {
        System.out.println(hi("Java"));
    }
}

import rules

  • import java.util.ArrayList; imports one type. import java.util.*; imports all types in that package, not subpackages.
  • java.lang is imported automatically. Subpackages like java.lang.reflect are not.
  • Two classes with the same simple name (java.util.Date and java.sql.Date) — import one and fully qualify the other.

Static import and access

  • import static java.lang.Math.PI; lets you write PI instead of Math.PI. Overuse hurts readability.
  • Package-private (no modifier) types and members are visible only inside the same package — a core encapsulation tool.
ModifierSame classSame packageSubclass (other pkg)World
privateYesNoNoNo
(package)YesYesNoNo
protectedYesYesYesNo
publicYesYesYesYes
L2

Advanced — Packages

Modules, unnamed package, and classpath

  • The default (unnamed) package cannot be imported. Do not leave real code there.
  • javac -d out src/demo/util/Greetings.java writes class files under out/demo/util/. Run java -cp out demo.util.Greetings.
  • Java 9+ modules (module-info.java) add stronger encapsulation (exports). Interviews still start with packages + access modifiers.
  • Wildcard imports do not hurt runtime; they only affect compile-time name lookup.
L3

Tricky Interview Points

Q1) What does a package do?

Answer It namespaces types, maps to a directory, and defines package-private visibility.

Q2) Can two public classes in different packages share a name?

Answer Yes. Fully qualify or import one. The JVM identity is package + class name.

Q3) Does import java.util.* import java.util.concurrent?

Answer No. Star imports only that package, not subpackages.

Q4) What is a static import?

Answer import static copies static members into simple-name scope, e.g. import static java.lang.Math.sqrt;

Q5) Where must the package statement appear?

Answer First in the file (after comments / Javadoc). Only one package declaration is allowed.

Q6) What is the default access modifier?

Answer Package-private: visible to all types in the same package, hidden from other packages.

L4

Simple Programs

Same-file demo of package naming (default package fallback)

import static java.lang.Math.max;

public class PackageDemo {
    public static void main(String[] args) {
        System.out.println(max(3, 8));
        java.util.List<String> names = java.util.List.of("a", "b");
        System.out.println(names);
    }
}
L5

Problem Solving Tricks

  • Always declare a package in project code; default package is for scratch files only.
  • IDE “run” hides classpath mistakes — practice javac -d and java -cp once.
  • Prefer single-type imports in tutorials so readers see where each class lives.
  • protected is for subclasses *and* the same package — people forget the package part.
L6

Practice Programs List

  1. Create pkg.alpha.A and pkg.beta.B where B calls a public method of A.
  2. Try to import a default-package class from a packaged class and read the error.
  3. Use a static import for Math.pow and print 2^10.
  4. Make a package-private class and attempt to use it from another package.
  5. Compile with -d out and run with -cp out from the project root.