Sub topic 8
Declaring Classes, Records, and Interfaces
This chapter assumes that you know basic Java variables, methods, classes, and constructors. You also need a JDK with jshell available. The class and interface examples work with Java 9 or later. The record example requires Java 16 or later.
Declaring a Class
A class declaration is a JShell snippet. You do not need to place the class in a .java file or add a main method.
jshell> class Temperature {
...> private final double celsius;
...>
...> Temperature(double celsius) {
...> this.celsius = celsius;
...> }
...>
...> double fahrenheit() {
...> return celsius * 9 / 5 + 32;
...> }
...> }
| created class Temperature
jshell> Temperature room = new Temperature(20)
room ==> Temperature@...
jshell> room.fahrenheit()
$2 ==> 68.0JShell recognizes that the class declaration is incomplete after the first line. The ...> prompt means that it is waiting for more input. The closing brace completes the snippet.
A class can use other declarations that already exist in the session. This makes JShell useful for trying a small design before creating source files.
Declaring Records
A record is a compact class for storing data. Its header declares components, and Java supplies a constructor, accessors, equals, hashCode, and toString.
jshell> record Point(int x, int y) {
...> int manhattanDistance() {
...> return Math.abs(x) + Math.abs(y);
...> }
...> }
| created record Point
jshell> Point point = new Point(3, -4)
point ==> Point[x=3, y=-4]
jshell> point.x()
$4 ==> 3
jshell> point.manhattanDistance()
$5 ==> 7Record accessors use the component name, such as point.x(). They do not use the usual getX() form. Record components are final, so a record is appropriate when the object should not change after construction.
Declaring Interfaces and Implementations
An interface defines operations without choosing the implementation. A class implements the interface and supplies those operations.
jshell> interface Formatter {
...> String format(String input);
...> }
| created interface Formatter
jshell> class UpperFormatter implements Formatter {
...> public String format(String input) {
...> return input.toUpperCase();
...> }
...> }
| created class UpperFormatter
jshell> Formatter formatter = new UpperFormatter()
formatter ==> UpperFormatter@...
jshell> formatter.format("jshell")
$6 ==> "JSHELL"The variable type is Formatter, so the code uses the interface contract rather than depending on the implementation class.
Redefining Declarations
You can declare a class, record, or interface again with the same name while experimenting. JShell replaces the declaration for later input. Objects created from the earlier declaration are not retroactively changed, so recreate test objects after redefining a type.
Exercise
Declare a record named Size with int width and int height components, then create a Size value for 800 by 600 and call its width accessor.
Show answer
Use record Size(int width, int height) { }, then Size screen = new Size(800, 600); and screen.width().