Hi everyone.
Welcome back.
In this lecture we will learn about the record classes.
I will start with some theory first and then we will get to the coding part.
Record class is a special type of class with a record keyword in it, instead of the class keyword that
we normally use to create classes.
They are immutable data holders, which means the primary goal and intention of record classes is to
just hold the data.
So here is an example of how a record class would look like.
So in this case we have a record class named product.
And you can notice there is a record keyword in it instead of the class keyword.
And followed by that we have the open and close parentheses which holds a field that product is going
to represent.
We have three fields here name, cost and type to represent the product.
But in reality there can be many more fields to represent a product that a user is interested in buying.
So the final version of the record class is available from Java 17.
Let's say your code runs on Java 17.
Then you already have the record feature available for you to use.
And one important thing to call out is that record classes are final, which means no inheritance is
supported on the record classes.
And the record classes also have auto generated equals, hashCode and toString functions.
The idea is to make sure the class looks light, and the intention of the class is to behave as data
holders, but the auto generated functions is pretty easy to do the equality check without the need
to write any complex logic.
So with this information, let's go ahead and look into the record classes in action.
So I'm going to switch back to IntelliJ and start coding this one.
There you go I'm back in IntelliJ.
So we are going to be working on the Explore latest Java Features project.
So in this case I'm going to create a package named Record or Records.
And then I'm going to create a class named product.
So you create the record class as like a regular class.
So you select the Java class option, but in the place of this pop up window, you select the option
as record, okay?
And then we provide the value as product.
There you go.
So in the product what we are going to do, we are going to be providing the three fields that we saw
in the slide.
So in this case number one is going to be string name.
And the next one is going to be the cost.
And then the next one is going to be string type okay.
So this is a very simple representation of the product.
So we have the record class ready.
Now it's time to test this one out.
So what I'm going to do I'm going to be creating a product test.
And then try to create an instance of product and try to access these properties in it.
So in this case let's go ahead and create a test.
So in this case I'm going to create a test named Product Test.
Click on that one.
And then I'm going to put this on the right.
So what we need is a test case right.
So let's go ahead and create a test case.
There you go.
So in this case I'm going to name the test case as create product.
So in this case it's going to be create product.
There you go.
And what we are going to do is we are going to be creating a new product.
So where product equal to new product.
And then pass the name as I'm going to create a product named iPhone.
And then I'm going to pass the cost as a new Bigdecimal.
So in this case it's going to be new Bigdecimal.
And then we're going to be passing the value as 999.99.
And then I'm going to pass the type as electronics.
So in this case it's going to be electronics.
Okay.
So we have the product created.
The next step is what can we do with this product instance.
So let's say I want to just print this value so we can do a product.
There you go.
And other questions that I have in my head is how do I access this one.
But predominantly if you have used domain classes, either you will have a complete implementation of
the getter and setter methods, or you might have used a library like Lombok in Java to auto generate
those classes.
So in this case, if you do a product dot, what you have is name cast and type.
So these are the accessors.
So these are the functions which give you access to those values for these fields.
And you can see there is no setter function which means the record classes are immutable.
Once it's created there is no way that you can edit that value.
So what I'm going to do is I'm going to perform some assertions.
So I'm going to call the name.
So I'm going to do assert equals assert equals.
And then the expected name is going to be.
iPhone.
So I'm going to provide that value over here.
There you go.
And then similarly for the type it's going to be electronics.
It's going to be type okay.
So now let's go ahead and execute this program.
So after we executed this test let's go ahead and quickly look into the console.
So in the console you see right.
So this two string is automatically generated for you.
The only reason why this is available to us is because we defined the class as record type.
So when you define the class as record type, it gives you access to the two string and it automatically
constructs this for you.
I hope you all were able to successfully create a record and how to construct a record.
And how do you access the properties that are part of the record in the next lecture?
What I'm going to do is I'm going to show you some advanced concepts.
Let's try to record meaning.
How do you perform the property validations?
Let's say you don't want to have an empty value as a product name, and you don't want the cost to be
in a negative value, right?
So all those validations can be applied before constructing the actual product instance.
Let's look into those concepts in the next lecture.
Before I wind up this lecture, I would like to quickly provide a summary of what we have learned.
I want to specifically cover the summary by explaining the benefits of using the record classes.
So with the introduction of record, the domain classes in Java can be represented in simpler form.
Because record concept is part of the language itself.
With the record classes, we don't have to write the boilerplate code for the domain classes.
This involves constructors getter, setter, hashCode, equals, and toString.
All these are automatically available for you if you define your domain classes with a record type.
This also avoids the need to rely on other libraries, such as Lombok to represent the domain classes,
and it's an alternative way to tackle the verbosity.
So the main concern that we are addressing as part of records is that code verbosity is a common complaint
by Java developers for creating and managing domain classes.
Record classes does a good job of handling it, and makes it a favorable choice to represent the domain
classes.
With this, we came to the end of this lecture.
Thank you for watching.