Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,330 --> 00:00:01,800
The next step is to try to get her.
2
00:00:03,070 --> 00:00:06,670
Every field needs a getter, the parts field is private, yet it doesn't have one.
3
00:00:07,210 --> 00:00:09,820
This means we have no way of accessing it from outside.
4
00:00:12,730 --> 00:00:15,700
So in this lesson, we're going to be together for the spare parts field.
5
00:00:18,910 --> 00:00:20,360
The getter is going to be public.
6
00:00:20,740 --> 00:00:22,510
It's going to return a string array.
7
00:00:25,870 --> 00:00:31,180
It's going to be lower, Campbell case, it'll start with the word get an end with the field name and
8
00:00:31,180 --> 00:00:31,720
that's all.
9
00:00:34,420 --> 00:00:37,570
Now, you might be tempted to write return this parts.
10
00:00:38,600 --> 00:00:42,050
Let's return it for now, return this stock parts.
11
00:00:51,210 --> 00:00:52,380
And now called the Geter.
12
00:00:56,260 --> 00:00:58,090
Nissan aget parts.
13
00:01:01,540 --> 00:01:05,050
And set a variable equal to the return value, just some random variable.
14
00:01:10,480 --> 00:01:14,590
OK, now I'm going to change index one of the outside variable to hello.
15
00:01:24,970 --> 00:01:30,130
And then we're going to access the field from the Nissan once again, and I'm going to print every element
16
00:01:30,130 --> 00:01:31,930
inside of it as one long string.
17
00:01:56,630 --> 00:02:02,930
All right, this is bad, I change the outside variable, but the field from the Nissan object got affected
18
00:02:02,930 --> 00:02:03,340
as well.
19
00:02:05,090 --> 00:02:10,610
If you return the field, what happened is that Jova returns a copy of what's inside and what's inside
20
00:02:10,610 --> 00:02:11,600
is a reference.
21
00:02:12,120 --> 00:02:17,570
And so the getter is supposed to protect your field, but it's returning a reference that points directly
22
00:02:17,570 --> 00:02:18,200
to it.
23
00:02:19,270 --> 00:02:25,630
This actually defeats the purpose of a geter, now the outside variable and field share a reference
24
00:02:25,630 --> 00:02:26,530
to the same array.
25
00:02:27,280 --> 00:02:30,730
If I update the outside variable, the field gets affected as well.
26
00:02:34,010 --> 00:02:40,040
This is something I like to call reference, trapped three year geter should not return an array field
27
00:02:40,040 --> 00:02:40,630
directly.
28
00:02:41,060 --> 00:02:45,180
The pitfall is that it will return a reference that points directly to the field.
29
00:02:45,890 --> 00:02:48,980
So we need to make sure that they get a returns, a copy of the array.
30
00:02:49,880 --> 00:02:51,580
This table is going to be in your cheat sheet.
31
00:02:51,590 --> 00:02:52,160
Don't worry.
32
00:02:54,380 --> 00:02:59,720
So back to our Geter, we're going to use a raised copy of to create a new copy of our field.
33
00:03:18,340 --> 00:03:20,740
And we're going to copy the entire length of the array.
34
00:03:24,020 --> 00:03:25,430
All right, now run the code.
35
00:03:33,370 --> 00:03:37,930
And splendid, changing the outside variable has no effect on the field.
36
00:03:41,800 --> 00:03:46,960
And that's because the getter returns a new copy of the array field, and so now you're done adding
37
00:03:46,960 --> 00:03:49,210
the getter and your code is bulletproof.
38
00:03:49,600 --> 00:03:53,110
There is no chance of you accidentally changing any of your fields.
39
00:03:53,540 --> 00:03:59,470
Every field is protected by the private keyword and each getter returns a secure copy of the field.
40
00:04:02,300 --> 00:04:06,770
OK, so now we can remove the random variable we made, it was just for testing purposes.
41
00:04:09,080 --> 00:04:12,920
But there's still one issue try printing the parts field from Nissan to.
42
00:04:22,960 --> 00:04:25,030
And we get a null pointer exception.
43
00:04:29,310 --> 00:04:34,080
That's because the parts field for the Nissan object or for the Nissan to object, I should say, is
44
00:04:34,080 --> 00:04:39,720
null, and attempting to access or call something from a null causes a crush, an exception.
45
00:04:41,810 --> 00:04:45,620
And that's why the crash is happening inside line 41 of the car class.
46
00:04:49,560 --> 00:04:52,770
So you might be asking, why is Nissan TEUs Hartsfield null?
47
00:04:53,520 --> 00:04:54,590
I'll give you a hint.
48
00:04:54,600 --> 00:04:59,040
Think about which constructor the Nissan two object uses to update its fields'.
49
00:05:02,780 --> 00:05:03,680
I hope you got it.
50
00:05:04,010 --> 00:05:06,620
We're going to talk more about this before moving on to step three.
51
00:05:09,790 --> 00:05:14,830
In this lesson, we added a GETER for the parts field getters are supposed to return secure copies of
52
00:05:14,830 --> 00:05:20,080
each field, so you have to be careful about returning a reference that points directly to your field.
53
00:05:20,830 --> 00:05:25,780
If you return the field, Java returns a copy of what's inside and what's inside is a reference.
54
00:05:26,380 --> 00:05:28,120
This defeats the purpose of the GETER.
55
00:05:28,540 --> 00:05:33,750
The Geter are supposed to protect our field, but it returns a reference that points directly to it.
56
00:05:34,060 --> 00:05:38,060
And now the outside variable has direct access to the same array as your field.
57
00:05:38,080 --> 00:05:39,770
And this is really dangerous.
58
00:05:40,360 --> 00:05:42,130
This is reference Tropp three.
59
00:05:42,670 --> 00:05:45,250
Your Geter should not return an array field directly.
60
00:05:45,820 --> 00:05:49,510
The pitfall is it's going to return a reference that points directly to the field.
61
00:05:50,170 --> 00:05:53,410
So we need to make sure that the getter returns a copy of the array.
62
00:05:55,800 --> 00:05:59,650
We did that in our code, the GETER created a copy of the field and returned it.
63
00:06:00,120 --> 00:06:03,720
Ultimately, the getter keeps your field safe by returning a copy of it.
6303
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.