Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You can enforce some invariants during construction:

    record Point(int x, int y) {
        Point {
            if (x < 0) throw new IllegalArgumentException()
        }
    }
or if you want to assert something is not null:

    record Person(String name) {
        Person {
            requireNonNull(name);
        }
    }


I think records will be much more useful if https://openjdk.org/jeps/468 gets out of preview.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: