Sub topic 10
Adding Helper Classes, Records, and Interfaces
This chapter assumes you know basic Java syntax, including classes, fields, methods, and interfaces. You should also understand the traditional public static void main(String[] args) method and know how to run a Java source file from the command line.
A compact source file is not limited to one method. You can add helper types, such as classes, records, and interfaces. Java treats these declarations as members of the implicitly declared class.
Helper types belong to the implicit class
Consider this compact source file:
record Student(String name, int score) {
}
interface ScoreLabel {
String label(int score);
}
class ReportFormatter {
String format(Student student, ScoreLabel scoreLabel) {
return student.name() + ": " + scoreLabel.label(student.score());
}
}
void main() {
Student student = new Student("Ada", 95);
ScoreLabel scoreLabel = score -> {
if (score >= 90) {
return "excellent";
}
return "passing";
};
ReportFormatter formatter = new ReportFormatter();
IO.println(formatter.format(student, scoreLabel));
}Save the file as Report.java and run it with Java 25:
java Report.javaThe output is:
Ada: excellentThe record stores a student's name and score. A record automatically provides a constructor, access methods such as name() and score(), and useful implementations of toString, equals, and hashCode.
The ScoreLabel interface defines one operation. The lambda expression in main supplies that operation.
The ReportFormatter class contains behavior that does not need to be part of main. The main method creates it with new, just as it would create an ordinary class.
In this file, all three helper types are members of the implicitly declared class. They are not unrelated top-level types in the same compilation unit.
Choosing a record, class, or interface
Use a record when the main purpose of a type is to carry data. Records are concise and communicate that their components form the object's state.
Use a class when a type needs mutable state, several implementation details, or behavior that does not fit naturally into a record.
Use an interface when callers should depend on an operation rather than on one particular implementation. In the example, main only needs something that can label a score. It does not need to know how that label is produced.
| Type | Best use | Example |
|---|---|---|
| Record | Small immutable data object | `Student(String name, int score)` |
| Class | Reusable state or behavior | `ReportFormatter` |
| Interface | A required operation or capability | `ScoreLabel` |
Converting the file to an explicit class
As the program grows, give the enclosing class an explicit name:
public class Report {
record Student(String name, int score) {
}
interface ScoreLabel {
String label(int score);
}
class ReportFormatter {
String format(Student student, ScoreLabel scoreLabel) {
return student.name() + ": " + scoreLabel.label(student.score());
}
}
void main() {
Student student = new Student("Ada", 95);
ScoreLabel scoreLabel = score -> score >= 90 ? "excellent" : "passing";
ReportFormatter formatter = new ReportFormatter();
IO.println(formatter.format(student, scoreLabel));
}
}The helper declarations remain nested inside Report, so the program's structure and behavior are unchanged. This is a useful migration step when a compact source file needs more organization.
Exercise
Which declaration is the best choice for an immutable value containing a customer's name and account number: a record, a class, or an interface?
Show answer
A record is the best choice because the declaration primarily carries fixed data and receives a constructor and access methods automatically.