Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
A Book class strips ' Dune ' to 'Dune' on the way in and formats itself on the way out. Small programs show the forgotten this (prints null), final on a reference, == vs equals and concat vs append, then a three-book aligned catalogue.
What you will learn
- Build an immutable Book with private final fields that cleans input with strip()
- Spot the forgotten-this bug that leaves a field null
- Tell what final locks and what concat vs append changes
- Produce an aligned report with formatted() and a StringBuilder
Before you start
- Java final and immutable objects
- Java strings as objects
This lab builds a small library catalogue and practises the last few lessons together: constructors and this, final, and strings as objects. It starts with a messy title, " Dune ", and ends with a neat three-column report.
The Book class
package org.studyeasy.s03l09;
public class Book {
private final String title;
private final String author;
private final int year;
public Book(String title, String author, int year) {
this.title = title.strip();
this.author = author.strip();
this.year = year;
}
public String getTitle() { return title; }
public String getAuthor() { return author; }
public int getYear() { return year; }
public String toString() {
return "%s by %s (%d)".formatted(title, author, year);
}
}
The fields are private final, so a book never changes after it's built. The constructor cleans the data once, on the way in: strip() trims spaces at both ends. toString formats it on the way out: formatted fills the %s, %s, %d placeholders in order.
package org.studyeasy.s03l09;
public class Ex01_NewBooks {
public static void main(String[] args) {
Book dune = new Book(" Dune ", " Frank Herbert", 1965);
Book emma = new Book("Emma", "Jane Austen", 1815);
System.out.println(dune);
System.out.println(emma);
System.out.println("Title length: " + dune.getTitle().length());
}
}
Dune by Frank Herbert (1965)
Emma by Jane Austen (1815)
Title length: 4
println calls toString for you. The title is 4 characters: 7 minus the 3 spaces. Remove .strip() from the constructor and it prints 7.
The forgotten this
package org.studyeasy.s03l09;
public class Ex02_ForgottenThis {
public static void main(String[] args) {
BookDraft b = new BookDraft("Dune");
System.out.println(b.title);
}
}
class BookDraft {
String title;
BookDraft(String title) {
title = title;
}
}
null
Inside the constructor, plain title means the parameter, on both sides. So the parameter is copied onto itself and the field is never set: it keeps its default, null. There's no error, which is what makes this bug sneaky. Write this.title = title; and the output becomes Dune (see constructors and this).
final locks the variable, not the object
package org.studyeasy.s03l09;
public class Ex03_FinalShelf {
public static void main(String[] args) {
final Shelf shelf = new Shelf();
shelf.count = 5;
shelf.count++;
// shelf = new Shelf(); // compile error: final
System.out.println("Count: " + shelf.count);
}
}
class Shelf {
int count;
}
Count: 6
shelf can only ever point to this one object, but count isn't final, so it goes to 5, then 6. Uncomment shelf = new Shelf(); and the compiler refuses. That's why Book marks its own fields final: so the book itself can't change.
== vs equals, concat vs append
String a = "Emma";
String b = "Emma";
String c = new String("Emma");
System.out.println("a == b: " + (a == b));
System.out.println("a == c: " + (a == c));
System.out.println("a.equals(c): " + a.equals(c));
a == b: true
a == c: false
a.equals(c): true
Two literals share one pooled object; new String makes another. == checks the object, equals the letters.
String title = "Emma";
title.concat("!");
StringBuilder note = new StringBuilder("Emma");
note.append("!");
System.out.println("title: " + title);
System.out.println("note: " + note);
title: Emma
note: Emma!
concat hands back a new String, but nothing stores it, so title is still Emma. append changes the builder itself. Write title = title.concat("!"); to keep the result.
The whole catalogue
package org.studyeasy.s03l09;
public class Lab3_Catalogue {
public static void main(String[] args) {
Book[] shelf = {
new Book(" Dune ", " Frank Herbert", 1965),
new Book("Emma", "Jane Austen", 1815),
new Book("Beloved", "Toni Morrison", 1987)
};
String row = "%-9s %-14s %4s%n";
StringBuilder report = new StringBuilder();
report.append(row.formatted("Title", "Author", "Year"));
for (Book b : shelf) {
report.append(row.formatted(b.getTitle(), b.getAuthor(), b.getYear()));
}
report.append("Books on shelf: " + shelf.length);
System.out.println(report);
}
}
Title Author Year
Dune Frank Herbert 1965
Emma Jane Austen 1815
Beloved Toni Morrison 1987
Books on shelf: 3
One pattern serves every row: %-9s pads the title to 9 characters, left-aligned; %-14s pads the author to 14; %4s gives the year 4; %n ends the line. A StringBuilder collects the header, one row per book and the count, and a single println prints it all. Change %-9s to %-4s and the columns stop lining up.
Recap
| Idea | In this lab |
|---|---|
| Clean on the way in | title.strip() in the constructor |
| Immutable class | private final fields, getters, no setters |
this.field | forget it and the field stays null |
final reference | locks the variable, not the object |
| String vs StringBuilder | concat returns new; append changes in place |
| Formatting | "%-9s %-14s %4s%n".formatted(...) |
Try it yourself: add a fourth book and a line at the end showing the oldest book on the shelf.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Java Classes and Objects: One Blueprint, Many Cars
Six loose variables for two cars doesn't scale. A Car class is the blueprint; new builds each car with its own brand, speed and fuel. Plus field defaults, and why two variables can point at one car.
Java Constructors and the this Keyword
A constructor decides how an object is born: new Car("Toyota", 42.5) arrives filled in. this.brand vs brand, a second constructor that calls this(brand, 50.0), and why new Car() stops compiling once you write your own.
Java Instance Methods, Getters, Setters and toString
Give a Car class behaviour: accelerate() changes only this car's speed, private fields are reached through getSpeed and setSpeed, and toString turns Car@1b6d3586 into 'Toyota doing 60 km/h'.
Java static: Fields, Methods and Blocks Shared by the Whole Class
A static field lives once on the class: every Car ticks the same carsBuilt counter while keeping its own brand. Static methods need no object, a static block runs before main, and main is static so the JVM can start with no object.
