Skip to content
Justice Tech School

Records

A record is a short immutable class with auto constructor, getters, equals and toString.

Example
public class Main {
  record Point(int x, int y) {}
  public static void main(String[] args) {
    Point p = new Point(1, 2);
    System.out.println(p + " " + p.x() + " " + p.equals(new Point(1, 2)));
  }
}

Result

Point[x=1, y=2] 1 true

Exercise

Declare a record.

 P(int x) {}

Quiz Test

Are record fields changeable?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.