Concept
A record declares an immutable data class in one line, with constructor, accessors, equals, hashCode and toString generated. var lets the compiler infer a local variable's type from its initialiser.
The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.
A few lines that show Records and var doing its job.
public record Money(String currency, long cents) {
public Money {
if (cents < 0) throw new IllegalArgumentException("negative amount");
}
public Money plus(Money other) {
return new Money(currency, cents + other.cents);
}
}
var total = new Money("USD", 1999).plus(new Money("USD", 1));
System.out.println(total); // Money[currency=USD, cents=2000]Using a record for something that must change over time, then adding setters through workarounds. A record's fields are final; model mutable state with a class.
Once a session shows you understand Records and var, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.
Topics that build on Records and var.