WEBVTT

00:00.140 --> 00:00.770
Hi everyone.

00:00.770 --> 00:01.340
Welcome back.

00:01.340 --> 00:04.490
In this lecture we will learn about the record classes.

00:04.520 --> 00:07.940
I will start with some theory first and then we will get to the coding part.

00:07.970 --> 00:14.030
Record class is a special type of class with a record keyword in it, instead of the class keyword that

00:14.030 --> 00:16.340
we normally use to create classes.

00:16.340 --> 00:23.510
They are immutable data holders, which means the primary goal and intention of record classes is to

00:23.540 --> 00:24.830
just hold the data.

00:24.830 --> 00:28.310
So here is an example of how a record class would look like.

00:28.310 --> 00:31.160
So in this case we have a record class named product.

00:31.160 --> 00:35.570
And you can notice there is a record keyword in it instead of the class keyword.

00:35.570 --> 00:42.500
And followed by that we have the open and close parentheses which holds a field that product is going

00:42.500 --> 00:43.490
to represent.

00:43.490 --> 00:47.960
We have three fields here name, cost and type to represent the product.

00:47.960 --> 00:54.590
But in reality there can be many more fields to represent a product that a user is interested in buying.

00:54.590 --> 00:58.910
So the final version of the record class is available from Java 17.

00:58.910 --> 01:01.430
Let's say your code runs on Java 17.

01:01.460 --> 01:05.510
Then you already have the record feature available for you to use.

01:05.510 --> 01:11.750
And one important thing to call out is that record classes are final, which means no inheritance is

01:11.750 --> 01:13.850
supported on the record classes.

01:13.880 --> 01:19.880
And the record classes also have auto generated equals, hashCode and toString functions.

01:19.880 --> 01:26.240
The idea is to make sure the class looks light, and the intention of the class is to behave as data

01:26.270 --> 01:32.240
holders, but the auto generated functions is pretty easy to do the equality check without the need

01:32.270 --> 01:33.950
to write any complex logic.

01:33.950 --> 01:38.330
So with this information, let's go ahead and look into the record classes in action.

01:38.330 --> 01:41.510
So I'm going to switch back to IntelliJ and start coding this one.

01:41.540 --> 01:42.950
There you go I'm back in IntelliJ.

01:42.980 --> 01:46.820
So we are going to be working on the Explore latest Java Features project.

01:46.820 --> 01:50.630
So in this case I'm going to create a package named Record or Records.

01:50.630 --> 01:53.000
And then I'm going to create a class named product.

01:53.000 --> 01:56.180
So you create the record class as like a regular class.

01:56.180 --> 02:01.220
So you select the Java class option, but in the place of this pop up window, you select the option

02:01.220 --> 02:02.240
as record, okay?

02:02.270 --> 02:04.580
And then we provide the value as product.

02:05.060 --> 02:05.990
There you go.

02:05.990 --> 02:10.460
So in the product what we are going to do, we are going to be providing the three fields that we saw

02:10.490 --> 02:11.600
in the slide.

02:11.600 --> 02:14.720
So in this case number one is going to be string name.

02:14.720 --> 02:17.450
And the next one is going to be the cost.

02:19.220 --> 02:23.090
And then the next one is going to be string type okay.

02:23.120 --> 02:26.120
So this is a very simple representation of the product.

02:26.120 --> 02:28.400
So we have the record class ready.

02:28.430 --> 02:30.380
Now it's time to test this one out.

02:30.380 --> 02:33.350
So what I'm going to do I'm going to be creating a product test.

02:33.350 --> 02:38.450
And then try to create an instance of product and try to access these properties in it.

02:38.450 --> 02:41.120
So in this case let's go ahead and create a test.

02:41.120 --> 02:44.330
So in this case I'm going to create a test named Product Test.

02:44.390 --> 02:45.380
Click on that one.

02:45.410 --> 02:47.360
And then I'm going to put this on the right.

02:49.550 --> 02:51.560
So what we need is a test case right.

02:51.590 --> 02:53.390
So let's go ahead and create a test case.

02:53.570 --> 02:54.440
There you go.

02:54.440 --> 02:57.950
So in this case I'm going to name the test case as create product.

02:57.980 --> 03:01.040
So in this case it's going to be create product.

03:01.070 --> 03:01.940
There you go.

03:01.940 --> 03:05.240
And what we are going to do is we are going to be creating a new product.

03:05.240 --> 03:09.410
So where product equal to new product.

03:09.410 --> 03:13.250
And then pass the name as I'm going to create a product named iPhone.

03:13.250 --> 03:17.180
And then I'm going to pass the cost as a new Bigdecimal.

03:17.180 --> 03:20.360
So in this case it's going to be new Bigdecimal.

03:20.360 --> 03:25.580
And then we're going to be passing the value as 999.99.

03:25.580 --> 03:28.070
And then I'm going to pass the type as electronics.

03:28.100 --> 03:31.100
So in this case it's going to be electronics.

03:31.910 --> 03:32.360
Okay.

03:32.390 --> 03:34.100
So we have the product created.

03:34.130 --> 03:37.040
The next step is what can we do with this product instance.

03:37.040 --> 03:40.700
So let's say I want to just print this value so we can do a product.

03:40.730 --> 03:41.330
There you go.

03:41.330 --> 03:45.170
And other questions that I have in my head is how do I access this one.

03:45.170 --> 03:51.710
But predominantly if you have used domain classes, either you will have a complete implementation of

03:51.710 --> 03:57.520
the getter and setter methods, or you might have used a library like Lombok in Java to auto generate

03:57.520 --> 03:58.330
those classes.

03:58.330 --> 04:03.430
So in this case, if you do a product dot, what you have is name cast and type.

04:03.430 --> 04:05.080
So these are the accessors.

04:05.080 --> 04:09.820
So these are the functions which give you access to those values for these fields.

04:09.820 --> 04:15.400
And you can see there is no setter function which means the record classes are immutable.

04:15.400 --> 04:18.940
Once it's created there is no way that you can edit that value.

04:18.940 --> 04:22.030
So what I'm going to do is I'm going to perform some assertions.

04:22.030 --> 04:23.710
So I'm going to call the name.

04:23.710 --> 04:28.360
So I'm going to do assert equals assert equals.

04:28.360 --> 04:31.270
And then the expected name is going to be.

04:33.400 --> 04:34.090
iPhone.

04:34.090 --> 04:36.190
So I'm going to provide that value over here.

04:37.180 --> 04:37.840
There you go.

04:37.840 --> 04:41.800
And then similarly for the type it's going to be electronics.

04:42.490 --> 04:45.580
It's going to be type okay.

04:45.610 --> 04:47.890
So now let's go ahead and execute this program.

04:49.660 --> 04:54.040
So after we executed this test let's go ahead and quickly look into the console.

04:54.040 --> 04:55.480
So in the console you see right.

04:55.480 --> 04:58.840
So this two string is automatically generated for you.

04:58.840 --> 05:05.140
The only reason why this is available to us is because we defined the class as record type.

05:05.140 --> 05:10.330
So when you define the class as record type, it gives you access to the two string and it automatically

05:10.330 --> 05:11.560
constructs this for you.

05:11.590 --> 05:16.660
I hope you all were able to successfully create a record and how to construct a record.

05:16.660 --> 05:20.110
And how do you access the properties that are part of the record in the next lecture?

05:20.110 --> 05:23.380
What I'm going to do is I'm going to show you some advanced concepts.

05:23.380 --> 05:24.910
Let's try to record meaning.

05:24.910 --> 05:27.340
How do you perform the property validations?

05:27.340 --> 05:33.010
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

05:33.010 --> 05:34.240
in a negative value, right?

05:34.240 --> 05:39.730
So all those validations can be applied before constructing the actual product instance.

05:39.730 --> 05:41.830
Let's look into those concepts in the next lecture.

05:41.830 --> 05:46.570
Before I wind up this lecture, I would like to quickly provide a summary of what we have learned.

05:46.750 --> 05:52.660
I want to specifically cover the summary by explaining the benefits of using the record classes.

05:52.660 --> 05:58.840
So with the introduction of record, the domain classes in Java can be represented in simpler form.

05:58.840 --> 06:02.740
Because record concept is part of the language itself.

06:03.010 --> 06:08.230
With the record classes, we don't have to write the boilerplate code for the domain classes.

06:08.260 --> 06:13.030
This involves constructors getter, setter, hashCode, equals, and toString.

06:13.030 --> 06:19.210
All these are automatically available for you if you define your domain classes with a record type.

06:19.240 --> 06:25.900
This also avoids the need to rely on other libraries, such as Lombok to represent the domain classes,

06:25.900 --> 06:28.930
and it's an alternative way to tackle the verbosity.

06:28.930 --> 06:34.840
So the main concern that we are addressing as part of records is that code verbosity is a common complaint

06:34.840 --> 06:38.920
by Java developers for creating and managing domain classes.

06:38.950 --> 06:45.040
Record classes does a good job of handling it, and makes it a favorable choice to represent the domain

06:45.040 --> 06:45.730
classes.

06:45.730 --> 06:47.560
With this, we came to the end of this lecture.

06:47.560 --> 06:48.700
Thank you for watching.
