Build an app with two packages
22 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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.
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.
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
// 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()); }}Issued: Java Basics Java Basics is already issued No book at shelf 5 Available: 2
What this code does
titlesandissuedare private: onlyBookShelfcan change them, andissuechecks every request first.issueandavailableCountare public, becauseLibraryAppin another package must call them. It importsBookShelfto 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.
javac -d out src/com/library/books/BookShelf.java src/com/library/app/LibraryApp.javajava -cp out com.library.app.LibraryAppA 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
// 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()); }}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_DAYSandPER_DAYare private; other classes need only the publicamount. LibraryAppnow imports one class from each package.BookShelf.javais unchanged from Example 1.- Add
src/com/library/members/Fine.javato thejavac -d outcommand too.
Next step
Check your understanding
8 quick questions