A small model in one file
The object part ends with a model small enough to read at once: a book, a shelf and a library. Three classes, one public, one file, and each class has one job. The example is the whole advice of the lesson: a class that holds data, a class that holds a collection of them, and a class that offers the operations of the application.
The model
Book is a value: a title and a page count, both final, a getter for each and a toString. Shelf holds up to ten books in an array and knows how to add one, count the pages and find a title. Library owns the shelf and offers the same operations to a caller that should not know there is a shelf at all.
Several classes in one file
Only one class per file may be public, and it gives the file its name. The other classes are package-private, which is enough when they live next to their user. A second public class needs a second file, and the build command then lists both: javac -Xlint:all Book.java Shelf.java Library.java.
The file Library.java:
public class Library {
private final Shelf shelf = new Shelf();
void add(Book book) {
shelf.add(book);
}
int pages() {
return shelf.pages();
}
Book find(String title) {
return shelf.find(title);
}
public static void main(String[] args) {
Library library = new Library();
library.add(new Book("Dune", 412));
library.add(new Book("Emma", 474));
System.out.println(library.find("Dune"));
System.out.println("pages: " + library.pages());
System.out.println("missing: " + library.find("Hamlet"));
}
}
class Shelf {
private final Book[] books = new Book[10];
private int size;
void add(Book book) {
books[size++] = book;
}
int pages() {
int total = 0;
for (int i = 0; i < size; i++) {
total += books[i].pages();
}
return total;
}
Book find(String title) {
for (int i = 0; i < size; i++) {
if (books[i].title().equals(title)) {
return books[i];
}
}
return null;
}
}
class Book {
private final String title;
private final int pages;
Book(String title, int pages) {
this.title = title;
this.pages = pages;
}
String title() {
return title;
}
int pages() {
return pages;
}
@Override
public String toString() {
return title + " (" + pages + ")";
}
}
javac -Xlint:all Library.java
java Library
Dune (412)
pages: 886
missing: null
The report
The page count is 886, the sum of 412 and 474, and it is computed by one loop over the filled part of the array. The size field is what makes the array a shelf of any length up to ten: the loop stops at size, not at books.length, so the empty slots never count.
The delegation is visible in three methods with the same names. A caller of Library.pages() does not know that the sum is in Shelf, and the shelf can change its storage without a change to the library or to the caller.
A missing book
find returns null for a title that is not on the shelf, and the concatenation prints the word null. That is the convention of this model; a caller that must distinguish the two cases checks the result before it uses it, and a model that wants to avoid the null returns an Optional instead, which the functional part of the course covers.
A model of three classes is where the decisions of lessons 034 to 045 meet: fields are final when they can be, classes hold what they own, methods delegate, and the public class is the one the application talks to.
Exercises
- Add a remove(String title) method to Shelf and delegate to it from the library.
- Make the shelf grow when the array is full by copying it to a longer one.
- Return Optional<Book> from find instead of a book that may be null.
- Split the three classes into three files with three public classes and adjust the build command.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How many public classes may one Java file hold?
Answer
One, the one that gives the file its name
A public class must be in a file of the same name, so a file can hold only one public class.
-
What does the field
sizein Shelf count?Answer
The books that were added
The array has ten slots and size says how many of them hold a book.
-
What does a search method return when it finds nothing in this model?
Answer
null
The method returns the reference it found or null, and the caller checks it.
-
What does this program print?
public class Q1 { public static void main(String[] args) { Shelf shelf = new Shelf(); shelf.add(new Book("A", 10)); shelf.add(new Book("B", 20)); System.out.println(shelf.pages()); } } class Book { final int pages; Book(String title, int pages) { this.pages = pages; } } class Shelf { private final Book[] books = new Book[10]; private int size; void add(Book book) { books[size++] = book; } int pages() { int total = 0; for (int i = 0; i < size; i++) { total += books[i].pages; } return total; } }Answer
30
The two books have 10 and 20 pages, and the loop stops at size 2.
-
What does this program print?
public class Q2 { public static void main(String[] args) { Shelf shelf = new Shelf(); shelf.add(new Book("A")); System.out.println(shelf.find("B") == null); } } class Book { final String title; Book(String title) { this.title = title; } } class Shelf { private final Book[] books = new Book[10]; private int size; void add(Book book) { books[size++] = book; } Book find(String title) { for (int i = 0; i < size; i++) { if (books[i].title.equals(title)) { return books[i]; } } return null; } }Answer
true
The shelf holds only A, so the search for B returns null and the test is true.
-
What does this program print?
public class Q3 { public static void main(String[] args) { Shelf shelf = new Shelf(); shelf.add(new Book("A", 5)); shelf.add(new Book("B", 7)); Book found = shelf.find("B"); System.out.println(found.pages); } } class Book { final String title; final int pages; Book(String title, int pages) { this.title = title; this.pages = pages; } } class Shelf { private final Book[] books = new Book[10]; private int size; void add(Book book) { books[size++] = book; } Book find(String title) { for (int i = 0; i < size; i++) { if (books[i].title.equals(title)) { return books[i]; } } return null; } }Answer
7
The search returns the second book, whose pages field is 7.
-
Why does Library keep the Shelf private?
Answer
The caller sees the operations, not the storage
A private field lets the storage change without changing the public operations.
-
What does this program print?
public class Q4 { public static void main(String[] args) { Book b = new Book("Dune", 412); System.out.println(b); } } class Book { private final String title; private final int pages; Book(String title, int pages) { this.title = title; this.pages = pages; } @Override public String toString() { return title + " (" + pages + ")"; } }Answer
Dune (412)
The override writes the title and the page count in parentheses.
-
What does a second class in the same file have to be?
Answer
Not public
Only the class that matches the file name may be public; the others are package-private.
-
Why does the loop in Shelf.pages() stop at
sizeand not atbooks.length?Answer
The slots after size are empty
The unused slots are null and have no pages to add.
The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.
Article author: Arthur Isaev