Java language
Record compact constructor
Canonical constructor form without an explicit parameter list; component fields are initialized after the body succeeds. Direct field assignments are not permitted in this form.
Formal Syntax & Structure
java syntaxjava
Product { /* validate or normalize parameters */ }Working Example
Example Snippetjava
public class Main { record Product(String code, int units) { Product { if (code == null || code.isBlank()) { throw new IllegalArgumentException("Code required"); } if (units < 0) throw new IllegalArgumentException("Negative units"); code = code.strip(); } } public static void main(String[] args) { Product item = new Product(" A12 ", 0); System.out.println(item.code() + ":" + item.units()); try { new Product("B", -1); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } }}