From the course: Top Features of Java 21
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Using unnamed patterns - Java Tutorial
From the course: Top Features of Java 21
Using unnamed patterns
- [Instructor] When we're coding in an object-oriented language like Java, it's often easy to end up with complex objects with lots of fields. So another new preview feature in Java 21 helps to remove boilerplate code when working with these more complex objects. So in this example, I have an interface called Building, which is just an empty interface. And then there are two classes that implement this, House and Store. So if I look inside the Store class, this is a record with a single field called address. And if I look inside the House class, this is also a record, but with three fields, address, number of bedrooms, and a Boolean called hasBasement. Finally, I have a class called Main with a main method in it. And inside my main method, I'm creating a house variable. And then I'm calling a method called printNumberOfBedrooms and passing in the house. So inside the printNumberOfBedrooms method, this takes a building…