What a record is
Some values belong together: a pet has a name and an age, a point has an x and a y. Java keeps values like these in a record, a small class whose only job is to hold them. One line declares it:
record Pet(String name, int age) {}
The values go in the parentheses, each with its type. The body {} is empty because Java writes the constructor and the reading methods for you. A record is a class, so it is declared outside Solution, not inside a method.
Making and reading a record
Make one with new, giving the values in the declared order. Read each value by its name, with parentheses, because the names are methods:
Pet rex = new Pet("Rex", 4);
System.out.println(rex.name());
System.out.println(rex.age());
System.out.println(rex);
This prints:
Rex
4
Pet[name=Rex, age=4]
Printing the record shows all its values. Two records with the same values are equal: rex.equals(new Pet("Rex", 4)) is true.
Gotchas
- Parentheses are required.
rex.age()reads the age. Writingrex.ageis a compile error,age has private access in Pet, because the field itself is hidden. - A record can't be changed. Its values are fixed when it is made. To change one, make a new record:
new Pet("Rex", 5)prints asPet[name=Rex, age=5]. - No one-line unpacking. Java can't split a record into variables in a single statement here. Copy each value you need:
String name = rex.name();.
Why a record and not two separate variables
Two loose variables, String petName and int petAge, work until you have to pass a pet to a method, return one, or keep several in a list. Then the two values have to travel together by hand, and it is easy to mix up the name of one pet with the age of another. A record makes the pair one value with a type of its own, Pet, so a method can take a Pet and a list can hold Pet items. Printing such a list shows each record in full: [Pet[name=Luna, age=3], Pet[name=Coco, age=9], Pet[name=Milo, age=9]].
Because the names belong to the record, the code that reads it says what each value means. pet.age() is clearer than the second item of an array.
Returning two values
A method can return only one value. To give back two, it returns a record that holds both. That is the idea behind the lesson's task: splitTime turns a number of minutes into hours and minutes left over.
The lesson's task step by step
- Declare
record Time(int hours, int mins) {}below thePetrecord, outsideSolution. - In
splitTime, return a newTimebuilt fromminutes / 60(whole hours, because integer division drops the remainder) andminutes % 60(the minutes left). For 135 that gives 2 and 15. - Call
splitTime(135)and keep the result in aTimenamedwalk.
The solution in Java
record Pet(String name, int age) {}
record Time(int hours, int mins) {}
class Solution {
public Time splitTime(int minutes) {
return new Time(minutes / 60, minutes % 60);
}
public void mainFunction() {
Pet pet = new Pet("Rex", 4);
System.out.println(pet.name());
System.out.println(pet.name() + " is " + pet.age());
Time walk = splitTime(135);
System.out.println(pet.name() + " walked " + walk.hours() + " h " + walk.mins() + " min");
}
}
The output is:
Rex
Rex is 4
Rex walked 2 h 15 min
Other inputs behave as you would expect: splitTime(45) gives Time[hours=0, mins=45] and splitTime(60) gives Time[hours=1, mins=0].
Key takeaways
- A record groups values under names with one declaration.
- Read values with
name()-style calls; never change them, make a new record instead. - Return a record when a method has two answers.
