Skip to content

Commit f92ca55

Browse files
authored
Merge pull request eugenp#10245 from michael-pratt/BAEL-4663
BAEL-4663: Whats new in Java 15
2 parents e5d779d + 9f95885 commit f92ca55

5 files changed

Lines changed: 51 additions & 2 deletions

File tree

core-java-modules/core-java-15/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
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>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.whatsnew.sealedclasses;
2+
3+
public final class Manager extends Person {
4+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

0 commit comments

Comments
 (0)