Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 8 of 10

Packages & Imports - Organising Code

Packages are namespaces that organise related classes into directories. They prevent name conflicts, control access visibility, and mirror Java's project folder structure. Understanding packages is essential for working in any real Java project.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Package Structure & Imports

Package Structure & Imports
// Package declaration - FIRST statement in file
// Convention: reverse domain + project + module
package com.priygop.library.model;

// Import specific class
import java.util.ArrayList;
import java.util.List;

// Import all classes in a package (avoid in production - reduces clarity)
// import java.util.*;

// Static import - import a static method/field directly
import static java.lang.Math.sqrt;
import static java.lang.System.out;

public class Book {
    private String isbn;
    private String title;

    // Now can use 'sqrt' directly without 'Math.sqrt'
    public double calculateSomething(double value) {
        return sqrt(value);  // == Math.sqrt(value)
    }

    // 'out' used instead of 'System.out'
    public void print() {
        out.println("Book: " + title);  // == System.out.println(...)
    }
}

// Typical library project structure:
// src/
// └── com/priygop/library/
//     ├── model/        Book.java, Member.java
//     ├── service/      LibraryService.java, BorrowingService.java
//     ├── repository/   BookRepository.java
//     ├── exception/    BookNotFoundException.java
//     └── Main.java

Quick Quiz

Tip

Tip

Practice Packages Imports Organising Code in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Packages & Imports - Organising Code

Practice Task

Note

Practice Task - (1) Write a working example of Packages Imports Organising Code from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Packages Imports Organising Code is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Topics in This Module

0/10 done