Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,180 --> 00:00:03,930
The constructor is the first thing that runs when you create an object.
2
00:00:05,620 --> 00:00:08,710
Having a constructor allows us to create the object.
3
00:00:10,770 --> 00:00:14,160
And update its fields on the exact same line.
4
00:00:17,200 --> 00:00:22,840
This is a big improvement on our previous code where we created the object and then in four separate
5
00:00:22,840 --> 00:00:24,850
lines, we updated its fields.
6
00:00:24,940 --> 00:00:27,280
This looks really disorganized.
7
00:00:28,970 --> 00:00:34,880
So it stands to reason that in this lesson you will use a constructor to update an object as Fields.
8
00:00:36,500 --> 00:00:39,500
The constructor runs as soon as you create an object.
9
00:00:40,940 --> 00:00:45,050
And the purpose of a constructor is to update an object as Fields.
10
00:00:46,850 --> 00:00:48,230
Let's go through the process.
11
00:00:48,230 --> 00:00:51,830
Step one is to create a constructor that defines four parameters.
12
00:00:54,320 --> 00:00:59,420
And to make a constructor, you need to specify the level of access which tends to be public.
13
00:01:01,010 --> 00:01:02,120
The class name.
14
00:01:04,069 --> 00:01:05,750
Followed by parameters.
15
00:01:08,070 --> 00:01:13,050
In step two, you have to call the constructor while creating a new car object.
16
00:01:15,130 --> 00:01:18,280
New car creates a new object of the car class.
17
00:01:21,830 --> 00:01:27,440
And while creating the car object by passing in four arguments, you're calling the constructor that
18
00:01:27,440 --> 00:01:33,020
expects to receive four parameters, and inside of the constructor is where you should update the newly
19
00:01:33,020 --> 00:01:35,090
created objects as fields.
20
00:01:37,020 --> 00:01:37,650
And that's why.
21
00:01:37,710 --> 00:01:41,970
Step three is that the constructor must update the objects as fields.
22
00:01:42,980 --> 00:01:43,820
The key word.
23
00:01:43,820 --> 00:01:46,610
This refers to the current object.
24
00:01:48,840 --> 00:01:53,460
The purpose of this is to distinguish between fields and parameters.
25
00:01:55,410 --> 00:02:00,780
You'll notice here that the parameters have the exact same name as the object as Fields.
26
00:02:03,050 --> 00:02:09,350
So we can use the this keyword to refer to the current object, the object that called the constructor.
27
00:02:11,680 --> 00:02:16,270
And it follows that this make refers to the field in that object.
28
00:02:17,710 --> 00:02:21,040
And we can set that field equal to the make parameter.
29
00:02:23,060 --> 00:02:27,350
In turn, we can set every field equal to the parameter that was passed in.
30
00:02:31,940 --> 00:02:32,900
And that's all.
31
00:02:33,020 --> 00:02:38,600
Once the constructor is done updating the objects as fields, the object has been fully initialized
32
00:02:38,600 --> 00:02:41,750
and our variable will store a reference to it.
33
00:02:43,770 --> 00:02:45,570
All right, let's put all of this into code.
34
00:02:45,570 --> 00:02:49,380
Step one was to create a constructor that defines four parameters.
35
00:02:50,420 --> 00:02:56,060
The constructor will be public and the constructor will get called whenever we create objects of the
36
00:02:56,060 --> 00:02:57,170
car class.
37
00:02:58,340 --> 00:03:03,500
And whenever the constructor gets called, it's going to expect for parameters to be passed in.
38
00:03:03,830 --> 00:03:07,820
It's going to expect a make a price.
39
00:03:08,790 --> 00:03:09,720
A year.
40
00:03:11,150 --> 00:03:12,290
And a color.
41
00:03:19,420 --> 00:03:23,590
Step two is to call the constructor while creating a new car object.
42
00:03:24,760 --> 00:03:30,250
So now that we've created our own constructor, Java is going to expect us to call it after we create
43
00:03:30,250 --> 00:03:31,930
a new car object.
44
00:03:31,930 --> 00:03:36,880
And we can call the constructor by passing in an equal number of arguments.
45
00:03:38,630 --> 00:03:48,200
We will pass any make of Nissan a price of 10,000 a year of 2020 and a colour of green.
46
00:03:49,770 --> 00:03:53,580
And now, instead of updating, the object is fields in four separate lines.
47
00:03:53,580 --> 00:03:56,370
The constructor is going to do it in a single line.
48
00:03:57,820 --> 00:03:59,110
And do the same thing over here.
49
00:03:59,110 --> 00:04:04,570
After we create a new object of the car class, we want the constructor that expects four parameters
50
00:04:04,570 --> 00:04:05,540
to get called.
51
00:04:05,560 --> 00:04:14,950
So here we're going to pass in four values Dodge 11,000, 2019, and Blue.
52
00:04:15,810 --> 00:04:19,589
And now, instead of updating, the object is fields in four separate lines.
53
00:04:19,589 --> 00:04:22,470
The constructor is going to do it in a single line.
54
00:04:23,460 --> 00:04:24,480
Looking good.
55
00:04:24,870 --> 00:04:29,250
And now step three is for the constructor to update the objects as fields.
56
00:04:31,210 --> 00:04:37,150
Every object that we define from the car class, it's going to have a field that matches a parameter
57
00:04:37,150 --> 00:04:38,100
in the constructor.
58
00:04:38,110 --> 00:04:42,400
So what we can do to distinguish between them is say this.
59
00:04:42,820 --> 00:04:49,930
So this refers to the current object that's calling this constructor, which means that this make refers
60
00:04:49,930 --> 00:04:51,640
to the field in that object.
61
00:04:51,640 --> 00:04:55,330
And here we can set the current objects as field equal to them.
62
00:04:55,330 --> 00:05:02,050
Make that gets passed in here, we can set the current object price field equal to the price value that
63
00:05:02,050 --> 00:05:03,400
gets passed in here.
64
00:05:03,400 --> 00:05:10,300
We can set the current objects year field equal to the year of value that gets passed in, and here
65
00:05:10,300 --> 00:05:13,210
we can set the current object as color field.
66
00:05:14,510 --> 00:05:18,350
Equal to the color value that we pass into our constructor.
67
00:05:19,130 --> 00:05:20,080
And that's really it.
68
00:05:20,090 --> 00:05:26,150
Whenever we create a new object of the car class, the constructor that expects to receive four values
69
00:05:26,150 --> 00:05:31,160
is going to get called, and it's going to update every single field in that object.
70
00:05:31,190 --> 00:05:33,250
This is looking really good.
71
00:05:33,260 --> 00:05:36,260
Let us visualize the runtime to see this in action.
72
00:05:38,400 --> 00:05:42,000
So here we're creating a new object of the car class.
73
00:05:42,030 --> 00:05:48,240
If I press a step inside, it's going to step inside the constructor that expects to receive four values.
74
00:05:49,180 --> 00:05:50,980
The make is a Nissan.
75
00:05:51,010 --> 00:05:53,380
The price is 10,000.
76
00:05:53,410 --> 00:05:57,700
The year is 2020 and the color is green.
77
00:05:58,420 --> 00:06:01,390
You can also visualize the parameter values over here.
78
00:06:01,420 --> 00:06:07,040
This points to the current object that we just created the new car object.
79
00:06:07,060 --> 00:06:10,600
By default, all of the fields start at zero or null.
80
00:06:11,460 --> 00:06:18,860
So inside the constructor we set every single field inside of the current object equal to a parameter.
81
00:06:18,870 --> 00:06:20,760
The start make equals make.
82
00:06:21,910 --> 00:06:25,720
This price equals the price value that we passed in.
83
00:06:26,820 --> 00:06:30,810
This year equals the year value that we passed in.
84
00:06:32,090 --> 00:06:36,050
And this color equals the color value that we passed in.
85
00:06:37,500 --> 00:06:37,950
All right.
86
00:06:37,950 --> 00:06:44,070
Once the constructor is done updating the object to Fields, the object has been fully initialized.
87
00:06:45,910 --> 00:06:49,840
So now our variable Nissan is going to store a reference to it.
88
00:06:51,990 --> 00:06:55,800
And now here, once again, we're creating a new object of the car class.
89
00:06:55,830 --> 00:07:01,680
If I press step into, it's going to step inside the constructor that expects to receive four values.
90
00:07:02,400 --> 00:07:06,060
This refers to the current object that we just created.
91
00:07:06,090 --> 00:07:09,220
Here we can see the parameter values that were passed in.
92
00:07:09,240 --> 00:07:16,050
Here we're setting this make the current objects is make field equal to the make value that we passed
93
00:07:16,050 --> 00:07:17,190
in Dodge.
94
00:07:19,480 --> 00:07:19,810
Here.
95
00:07:19,810 --> 00:07:22,570
We're setting the price field of the current object.
96
00:07:22,570 --> 00:07:27,550
This price equal to the price that we passed in 11,000.
97
00:07:27,760 --> 00:07:34,720
Here we're setting the year field of the current object this year equal to the year value that we passed
98
00:07:34,720 --> 00:07:35,140
in.
99
00:07:37,100 --> 00:07:40,670
And here we're setting the color field of the current object.
100
00:07:42,570 --> 00:07:46,020
Equal to the color that was passed in blue.
101
00:07:48,010 --> 00:07:54,970
And once the constructor is done updating the current objects as fields, now the object has been fully
102
00:07:54,970 --> 00:07:59,860
initialized and our dodge variable will store a reference to that object.
103
00:08:00,100 --> 00:08:02,020
So now we've got two variables.
104
00:08:02,020 --> 00:08:06,550
Each variable stores a reference that points to a brand new object.
105
00:08:06,700 --> 00:08:09,340
And here we can print the field values of each object.
106
00:08:09,340 --> 00:08:16,550
So our Nissan has a make of Nissan, a price of 10,000 a year of 2020 and a color of green.
107
00:08:16,570 --> 00:08:19,720
That is exactly what should reflect in our print function.
108
00:08:20,410 --> 00:08:24,910
Here we print every single field of the object that our Dodge variable points to.
109
00:08:24,940 --> 00:08:31,420
So this object is going to have a make of dodge, a color of blue, a price of 11,000 and a year of
110
00:08:31,420 --> 00:08:32,350
2019.
111
00:08:36,630 --> 00:08:38,740
And that's really it in this lesson.
112
00:08:38,760 --> 00:08:42,000
You used a constructor to update an object's fields.
113
00:08:43,929 --> 00:08:47,260
New car creates a new object of the car class.
114
00:08:48,410 --> 00:08:53,810
By passing in four values, we called the constructor that expects to receive four parameters.
115
00:08:55,240 --> 00:09:00,480
This points to the current object, the newly created object that called the constructor.
116
00:09:00,490 --> 00:09:06,790
So this dot make refers to the field in that object and we can set that field equal to the make parameter.
117
00:09:07,890 --> 00:09:12,960
And we can set each field in the current object equal to the parameter that was passed in.
118
00:09:15,910 --> 00:09:16,800
And that's really it.
119
00:09:16,810 --> 00:09:22,890
Once the constructor is done updating the object as fields, the object has been fully initialized and
120
00:09:22,900 --> 00:09:25,450
our variable will store a reference to it.
11727
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.