Class RecordPatternsExamples
java.lang.Object
net.jrodolfo.java_evolution.java21.RecordPatternsExamples
Demonstrates record patterns, finalized in Java 21.
Records make transparent data carriers concise, but code often needs to read
the components back out. Before record patterns, code first checked the type,
then called accessors such as rectangle.topLeft().x() to reach nested
values. Record patterns let code test the type and bind record components in
one expression, which is especially useful for nested records.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordPoint record used by the nested deconstruction example.static final recordRectangle record made of two points. -
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
RecordPatternsExamples
public RecordPatternsExamples()
-
-
Method Details
-
describe
Deconstructs nested records in theinstanceofcondition.If
shapeis aRecordPatternsExamples.Rectangle, the pattern also opens both nestedRecordPatternsExamples.Pointrecords and gives local names to their coordinates.- Parameters:
shape- the shape to describe- Returns:
- a text description
-