Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,300 --> 00:00:05,070
We need to fix our constructor so that it can update every single field, the car class, it's five
2
00:00:05,070 --> 00:00:05,660
fields.
3
00:00:06,060 --> 00:00:10,500
So when you create an object of the car class, the object is going to define Fifield's.
4
00:00:12,180 --> 00:00:14,550
However, your constructor only updates four of them.
5
00:00:24,330 --> 00:00:26,550
In this lesson, you're going to fix your constructor.
6
00:00:30,530 --> 00:00:35,990
Your constructor is missing a parameter, it's the constructor's job to update every field in the object,
7
00:00:36,410 --> 00:00:40,790
so your constructor needs to receive another parameter string parts.
8
00:00:45,300 --> 00:00:48,270
And now set the array field from the current object.
9
00:00:50,020 --> 00:00:55,120
This parts is equal to a copy of the perimeter erased that copy of.
10
00:01:05,760 --> 00:01:07,890
We're going to get a copy of the Pozza ray.
11
00:01:09,860 --> 00:01:16,460
And how much of it do we want to copy all of it parts just like remember to avoid setting array variables
12
00:01:16,460 --> 00:01:20,060
equal to each other, otherwise you'll fall into a reference trap.
13
00:01:20,630 --> 00:01:24,130
We talked about this table in Module one, but it's also in your cheat sheet.
14
00:01:26,880 --> 00:01:30,540
So inside main create a new array of parts, string parts.
15
00:01:37,860 --> 00:01:40,860
And it's going to store two parts, Tirhas and Keese.
16
00:01:44,970 --> 00:01:49,620
And the reason we have errors is because of the constructor is now expecting five parameters.
17
00:01:52,550 --> 00:01:54,530
And we're only passing in four values.
18
00:01:57,330 --> 00:02:00,690
So make sure to pass in the part array into each constructor call.
19
00:02:01,810 --> 00:02:02,820
OK, we're good.
20
00:02:04,070 --> 00:02:09,919
And before we run the code removed, the individual calls to drive the drive action is going to play
21
00:02:09,919 --> 00:02:10,770
its role later.
22
00:02:11,060 --> 00:02:12,110
Right now, we don't need it.
23
00:02:13,150 --> 00:02:14,140
Can't run the code.
24
00:02:27,210 --> 00:02:29,070
And these are not spare parts, is it?
25
00:02:29,070 --> 00:02:34,260
Not anymore, but instead of printing a reference to the array we're going to use to string to print
26
00:02:34,260 --> 00:02:36,810
the contents of the array arrays.
27
00:02:43,680 --> 00:02:44,490
To string.
28
00:02:47,060 --> 00:02:48,500
Nissan parts.
29
00:03:00,910 --> 00:03:01,440
Perfect.
30
00:03:01,480 --> 00:03:02,980
We're making a lot of progress.
31
00:03:03,980 --> 00:03:09,710
You said Nissan equal to when you object and passed in five values, Java runs the constructor with
32
00:03:09,710 --> 00:03:10,790
five parameters.
33
00:03:11,330 --> 00:03:15,460
The parts parameter copies, the value inside the parts argument, the reference.
34
00:03:15,830 --> 00:03:19,820
So the parts parameter and parts arguments share a reference to the same array.
35
00:03:23,850 --> 00:03:26,010
Every field updates with a new value.
36
00:03:29,350 --> 00:03:33,130
But notably, we set the parts field equal to a copy of the array.
37
00:03:40,070 --> 00:03:44,930
Now, I need to tell you to pay attention to the reference trap, the reference trap occurs when you
38
00:03:44,930 --> 00:03:46,800
mishandle arrays or objects.
39
00:03:47,510 --> 00:03:50,690
What would happen if we directly set the parts field equal to the parts?
40
00:03:50,690 --> 00:03:51,150
Brammer.
41
00:03:51,710 --> 00:03:55,580
Let's try this out in our code test that parts equal parts.
42
00:03:57,770 --> 00:03:58,800
This is dangerous.
43
00:03:59,090 --> 00:04:00,200
Now, the argument.
44
00:04:01,680 --> 00:04:05,520
The perimeter and field alshehri reference to the same array.
45
00:04:10,420 --> 00:04:15,160
By mistake, let's assume I changed index one from the outside variable parts.
46
00:04:16,250 --> 00:04:18,769
I'll change the next one and set it equal to a written.
47
00:04:34,670 --> 00:04:35,520
Do you see what I mean?
48
00:04:35,720 --> 00:04:40,880
I meant only update the outside variable, the argument we Pastan, but the Nissan field changed as
49
00:04:40,880 --> 00:04:41,180
well.
50
00:04:44,240 --> 00:04:50,630
Have a look, you said Nissan equal to a new object and passed in five values, Gever runs the constructor
51
00:04:50,630 --> 00:04:51,770
with five parameters.
52
00:04:52,340 --> 00:04:56,420
The parts parameter copies, the value inside the parts argument, a reference.
53
00:04:57,640 --> 00:05:04,180
So the parts parameter and parts arguments, a reference to the same array, we said every field equal
54
00:05:04,180 --> 00:05:05,050
to a parameter.
55
00:05:12,720 --> 00:05:17,610
The party field copies the value inside the parts parameter, also a reference.
56
00:05:18,150 --> 00:05:23,090
Now the argument parameter and field share a reference to the same array.
57
00:05:23,730 --> 00:05:26,640
The parameter only exists inside the constructor.
58
00:05:26,850 --> 00:05:28,740
So eventually it falls out of scope.
59
00:05:29,010 --> 00:05:32,560
But the field and outside variables still point to the same array.
60
00:05:32,910 --> 00:05:38,100
And that's why modifying the outside variable directly affected the state of my field.
61
00:05:41,390 --> 00:05:47,660
With that being said, I need to warn you that the reference drop is a recipe for bugs, when many variables
62
00:05:47,660 --> 00:05:51,740
reference the same thing, your application's behavior becomes unpredictable.
63
00:05:51,770 --> 00:05:54,890
Your variables are going to start changing when you don't expect them to.
64
00:05:55,520 --> 00:05:58,250
And you're always going to scratch your head saying what's going on?
65
00:06:01,330 --> 00:06:06,400
To avoid this headache, it's really simple, it should be really careful about how you handle arrays
66
00:06:06,400 --> 00:06:07,160
and objects.
67
00:06:07,690 --> 00:06:11,710
I encourage you to follow my tables and pay attention to the reference Trappe.
68
00:06:16,280 --> 00:06:20,330
So once again, I'm going to set the field equal to a copy of the parameter.
69
00:06:25,160 --> 00:06:30,200
This time around, changing the outside variable should have no effect on the field, a straight out.
70
00:06:35,370 --> 00:06:36,140
And there you go.
71
00:06:40,540 --> 00:06:45,010
All right, we have to make the field private again, every field needs to be private.
72
00:06:45,250 --> 00:06:46,900
The array field is no exception.
73
00:06:51,970 --> 00:06:56,950
And now we have areas in Maine you cannot access a private field, and that's good, you should never
74
00:06:56,950 --> 00:06:58,540
have direct access to a field.
75
00:06:59,050 --> 00:07:03,850
So for now, all we can do is delete the print statements and we're going to add get her in the next
76
00:07:03,850 --> 00:07:04,240
lesson.
77
00:07:07,280 --> 00:07:12,380
In this lesson, you fixed your constructor, the constructor is missing a parameter, so you added
78
00:07:12,380 --> 00:07:13,560
a parts parameter.
79
00:07:13,670 --> 00:07:18,440
Now your constructor is all good because as soon as you create a new object, your constructor updates
80
00:07:18,440 --> 00:07:19,750
every field inside of it.
7692
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.