Skip to content

Commit 8b3a092

Browse files
committed
BAEL-4663: Whats new in Java 15
1 parent 10fe8b5 commit 8b3a092

4 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package whatsnew.records;
2+
3+
/**
4+
* Java record with a header indicating 2 fields.
5+
*/
6+
public record Person(String name, int age) {
7+
8+
/**
9+
* Public constructor that does some basic validation.
10+
*/
11+
public Person {
12+
if (age < 0) {
13+
throw new IllegalArgumentException("Age cannot be negative");
14+
}
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package whatsnew.sealedclasses;
2+
3+
import java.util.Date;
4+
5+
public non-sealed class Employee extends Person {
6+
7+
public Date getHiredDate()
8+
{
9+
return new Date();
10+
}
11+
12+
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package whatsnew.sealedclasses;
2+
3+
public final class Manager extends Person {
4+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package whatsnew.sealedclasses;
2+
3+
import java.util.Date;
4+
5+
public sealed class Person permits Employee, Manager
6+
{
7+
/**
8+
* Demonstration of pattern matching for instanceof
9+
*
10+
* @param person A Person object
11+
* @return
12+
*/
13+
public static void patternMatchingDemo(Person person)
14+
{
15+
if(person instanceof Employee employee)
16+
{
17+
Date hiredDate = employee.getHiredDate();
18+
}
19+
20+
if(person instanceof Employee employee && employee.getHiredDate() != null)
21+
{
22+
Date hiredDate = employee.getHiredDate();
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)