File tree Expand file tree Collapse file tree
core-java-modules/core-java-15
src/main/java/com/baeldung/whatsnew Expand file tree Collapse file tree Original file line number Diff line number Diff line change 5151 <configuration >
5252 <release >${maven.compiler.release} </release >
5353 <compilerArgs >--enable-preview</compilerArgs >
54- <source >15 </source >
55- <target >15 </target >
54+ <source >14 </source >
55+ <target >14 </target >
5656 </configuration >
5757 </plugin >
5858 <plugin >
Original file line number Diff line number Diff line change 1+ package com .baeldung .whatsnew .records ;
2+
3+ /**
4+ * Java record with a header indicating 2 fields.
5+ */
6+ public record Person (String name , int age ) {
7+ /**
8+ * Public constructor that does some basic validation.
9+ */
10+ public Person {
11+ if (age < 0 ) {
12+ throw new IllegalArgumentException ("Age cannot be negative" );
13+ }
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .whatsnew .sealedclasses ;
2+
3+ import java .util .Date ;
4+
5+ public non-sealed class Employee extends Person {
6+ public Date getHiredDate () {
7+ return new Date ();
8+ }
9+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .whatsnew .sealedclasses ;
2+
3+ public final class Manager extends Person {
4+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .whatsnew .sealedclasses ;
2+
3+ import java .util .Date ;
4+
5+ public sealed class Person permits Employee , Manager {
6+ /**
7+ * Demonstration of pattern matching for instanceof
8+ *
9+ * @param person A Person object
10+ * @return
11+ */
12+ public static void patternMatchingDemo (Person person ) {
13+ if (person instanceof Employee employee ) {
14+ Date hiredDate = employee .getHiredDate ();
15+ }
16+
17+ if (person instanceof Employee employee && employee .getHiredDate () != null ) {
18+ Date hiredDate = employee .getHiredDate ();
19+ }
20+ }
21+ }
You can’t perform that action at this time.
0 commit comments