Skip to lesson
MentorNxt home

Java Programming

Packages, Imports & Access Control

Build an app with two packages

A small Java app is usually split by topic: one package holds the data and its rules, and another holds the class with main. A library app with the packages com.library.books and com.library.app is a good example.

Naming your packages

Two companies both call their package books, and their classes clash. Start every package with your website name written backwards, then the topic, all in lowercase: the website library.com gives com.library.books.

Classify

Is this a good package name?

Tap a package name, then pick how javac and other programmers see it.

Many apps keep the class with main in a package of its own, like com.library.app.

Plan the app by topic

A library app keeps book titles and which books are out, and its main issues books. Split it by topic: com.library.books holds the data and its rules, and com.library.app holds main.

Two packages. In com.library.app, LibraryApp has main. In com.library.books, BookShelf has public issue() and availableCount() and private titles and issued. LibraryApp calls the two public methods; the private arrays are blocked.

Choose access for each member

Which members of BookShelf should other packages see? Start with private, and make a member public only when another package must call it.

Example 1 · The library app

BookShelf.javaJava
// File: src/com/library/books/BookShelf.javapackage com.library.books;
public class BookShelf {    private static String[] titles = {"Wings of Fire", "Java Basics", "Godan"};    private static boolean[] issued = new boolean[3];
    public static void issue(int index) {        if (index < 0 || index >= titles.length) {            System.out.println("No book at shelf " + index);        } else if (issued[index]) {            System.out.println(titles[index] + " is already issued");        } else {            issued[index] = true;            System.out.println("Issued: " + titles[index]);        }    }
    public static int availableCount() {        int count = 0;        for (boolean b : issued) {            if (!b) {                count++;            }        }        return count;    }}
// File: src/com/library/app/LibraryApp.javapackage com.library.app;
import com.library.books.BookShelf;
public class LibraryApp {    public static void main(String[] args) {        BookShelf.issue(1);        BookShelf.issue(1);        BookShelf.issue(5);        System.out.println("Available: " + BookShelf.availableCount());    }}
Sample output
Issued: Java Basics
Java Basics is already issued
No book at shelf 5
Available: 2

What this code does

  • titles and issued are private: only BookShelf can change them, and issue checks every request first.
  • issue and availableCount are public, because LibraryApp in another package must call them. It imports BookShelf to use the short name.
  • Issuing book 1 twice is refused, and shelf 5 does not exist, so two of the three books are still available.

Use default access (no word) for a helper that only classes in the same package need, as in Access modifiers (opens in a new tab).

Compile and run the whole app

Open the terminal in the project folder. List every file after javac -d out, then run the class that has main by its full name. In an IDE, just run LibraryApp.

Terminal
javac -d out src/com/library/books/BookShelf.java src/com/library/app/LibraryApp.javajava -cp out com.library.app.LibraryApp

A new topic, a new package

The library now charges a fine for late books. A fine has nothing to do with the shelf, so it gets its own package, com.library.members, and BookShelf stays as it is.

Example 2 · Adding a members package

Fine.javaJava
// File: src/com/library/members/Fine.javapackage com.library.members;
public class Fine {    private static final int FREE_DAYS = 14;    private static final int PER_DAY = 2;
    public static int amount(int daysKept) {        if (daysKept <= FREE_DAYS) {            return 0;        }        return (daysKept - FREE_DAYS) * PER_DAY;    }}
// File: src/com/library/app/LibraryApp.javapackage com.library.app;
import com.library.books.BookShelf;import com.library.members.Fine;
public class LibraryApp {    public static void main(String[] args) {        BookShelf.issue(0);        System.out.println("Fine for 10 days: Rs " + Fine.amount(10));        System.out.println("Fine for 20 days: Rs " + Fine.amount(20));        System.out.println("Available: " + BookShelf.availableCount());    }}
Sample output
Issued: Wings of Fire
Fine for 10 days: Rs 0
Fine for 20 days: Rs 12
Available: 2

What this code does

  • The constants (opens in a new tab) FREE_DAYS and PER_DAY are private; other classes need only the public amount.
  • LibraryApp now imports one class from each package. BookShelf.java is unchanged from Example 1.
  • Add src/com/library/members/Fine.java to the javac -d out command too.

Next step

Check your understanding

8 quick questions