From the course: Java 25 for Professionals with Jetbrains AI Assistant

Writing more flexible constructors

- [Instructor] Another new feature of Java 25 are flexible constructors. The flexible constructors allow us to add logic-like validation before an explicit constructor invocation. Now you might think that doesn't sound too shocking and that it doesn't enable any new programming patterns. Hmm, that might be true, but it does allow for a more natural flow in our codes, and also method calls from the parent's constructors are now safer because we can have initialized the data in the child class already. I've prepared a little example for you. So here's the class bank account, which a bank account constructor, and as you can see, it's nothing too special here. We have an account number and a balance. Then savings account is actually where the magic is happening. So if we look at this construction of savings account that is starting on line seven, if I scroll down, you can see something that was not possible before. On line 26, I'm calling super the parent constructor, which is something that always had to be the very first call of a constructor. So with these flexible, constructive bodies, we allow logic such as validation initialization before calling the super class constructor. However, we cannot call any instance methods in here that's not allowed before calling another constructor. So what are we doing? Well, for example, on line 10, I'm checking the initial balance. If it's under a hundred, I'm throwing an illegal argument exception. On line 15 I'm checking if the interest is between one and 15%. If not, I'm showing an illegal argument exception, and then I'm initializing the fields before we call super. And that's great because that actually ensures integrity and that's making it safer to call methods in the parent constructor. Then I'm calling deconstructor and after calling deconstructor, I could do other things. In this case, I'm simply logging what happened. Here's an example of where this is relevant. So the log account creation is being called from the bank account constructor as well. It's available here, but when we call log account creation on a savings account object, it'll actually call the one in savings account. And since that one is using instance data, it would be uninitialized in the previous situation where we had to call super first, now it will be initialized. Let me show that to you with this demo. In this demo, I'm creating several bank accounts, starting with a valid savings account, one with insufficient balance, one with an invalid interest rate, and then one premium account. Alright, let's go ahead and run this. And you can see in the output, for example, the valid saving account, that is able to mention the balance in there already. And this is thanks to the new flexible constructive body. Also, we are failing a few times because we now allow it to fail before calling super, which is much better because we don't have any unfinished objects then. I can imagine you're very enthusiastic, so let's go ahead and get some hands-on practice with this yourself.

Contents