Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,480 --> 00:00:03,060
If a class has failed, you need to add getas.
2
00:00:05,480 --> 00:00:07,770
The cars is private, as it should be.
3
00:00:08,119 --> 00:00:10,620
This means we can't access the field from outside.
4
00:00:10,640 --> 00:00:12,410
So in this lesson, we're going to to get her.
5
00:00:16,010 --> 00:00:19,340
The dealership class has one field, so we need to add one Geter.
6
00:00:20,900 --> 00:00:23,570
So should ever get her be called get cars.
7
00:00:24,140 --> 00:00:29,630
I hope you guessed the answer no, because let's imagine a scenario where I only want to access the
8
00:00:29,630 --> 00:00:36,350
car at index zero, get cars, would create copies of every object in the car's array, but I only need
9
00:00:36,350 --> 00:00:36,860
one of them.
10
00:00:37,550 --> 00:00:39,100
This is a waste of memory.
11
00:00:39,350 --> 00:00:42,320
When you create new copies of objects, it's not free.
12
00:00:42,590 --> 00:00:43,700
It takes up memory.
13
00:00:43,700 --> 00:00:45,260
It takes up space in your computer.
14
00:00:45,510 --> 00:00:47,870
And so what if my array has a thousand cars?
15
00:00:48,260 --> 00:00:53,730
Are you going to copy a thousand car objects, return them for the sake of accessing one?
16
00:00:54,470 --> 00:00:58,310
Once again, this is typical to a class that manages an array of objects.
17
00:00:58,310 --> 00:01:01,840
You just never know how many objects are going to be inside of that array.
18
00:01:02,270 --> 00:01:05,269
And so it doesn't make sense to return every object at once.
19
00:01:07,420 --> 00:01:12,310
Instead, the gutter is going to return one car object from an index of your choice.
20
00:01:17,460 --> 00:01:23,160
So inside your dealership class at the get or get car public, it's going to return a car object.
21
00:01:25,410 --> 00:01:30,480
Its name will be Get Car and it will take one parameter, the index of the car you want to get.
22
00:01:34,820 --> 00:01:39,260
Finally, return the car object found at the index chosen by the user.
23
00:01:46,000 --> 00:01:48,190
And now we can access the cars that we want.
24
00:01:49,800 --> 00:01:52,350
In Maine and get the car object at index zero.
25
00:02:03,320 --> 00:02:06,000
And the GETER is going to return the card index zero.
26
00:02:06,140 --> 00:02:06,830
Pretty simple.
27
00:02:11,630 --> 00:02:14,290
Let's print the new car object and make sure everything works fine.
28
00:02:38,530 --> 00:02:43,750
And nice, when you print an object, Java knows to check if there's a two string method that returns
29
00:02:43,750 --> 00:02:45,070
a string of every field.
30
00:02:47,170 --> 00:02:48,330
Sounds great, right?
31
00:02:48,520 --> 00:02:52,840
Let's update the new cars, clarefield new car at color.
32
00:02:55,720 --> 00:02:56,230
Blue.
33
00:03:01,830 --> 00:03:07,170
And then we're going to access another copy of the car object that in zero and print that object instead.
34
00:03:18,660 --> 00:03:24,700
Well, what happened, this is bad, I change the outside variable, but the object at Index zero,
35
00:03:24,730 --> 00:03:26,390
the cars feel changed as well.
36
00:03:30,220 --> 00:03:36,580
If you return the object, Jaba returns a copy of what's inside the element and what's inside is a reference.
37
00:03:37,150 --> 00:03:41,650
The letter is supposed to protect your field, but it returns a reference that points directly to one
38
00:03:41,650 --> 00:03:42,610
of the objects.
39
00:03:43,370 --> 00:03:45,200
This defeats the purpose of a letter.
40
00:03:45,610 --> 00:03:49,870
Now the outside variable and field share a reference to the same object.
41
00:03:52,390 --> 00:03:58,410
This is the last reference trap, reference trap for your Geter should not return an object directly.
42
00:03:58,750 --> 00:04:04,060
The pitfall is that the outside variable and field are going to share a reference to the same object.
43
00:04:05,590 --> 00:04:08,930
The solution is simple return a new copy of the object.
44
00:04:09,730 --> 00:04:13,420
This table is in your cheat sheet, so we're going to return a new car object.
45
00:04:16,610 --> 00:04:22,370
And in brackets, put the source object you want to copy values from we would like to copy values from
46
00:04:22,370 --> 00:04:28,280
the car object and the index chosen by the user and right after creating a new car object or passing
47
00:04:28,280 --> 00:04:33,170
any source object so Java knows to run the copy constructor, that receives one as a parameter.
48
00:04:33,770 --> 00:04:38,270
And it's going to copy every field value from the source object into the one we just created.
49
00:04:39,450 --> 00:04:44,310
And then the GETER is going to return a new copy, which gives me comfort, because now I know that
50
00:04:44,310 --> 00:04:50,640
there's no way any of the objects inside the cars field are going to leak across the gutter, keeps
51
00:04:50,640 --> 00:04:54,900
our field safe by returning copies of the underlying car objects.
52
00:04:56,100 --> 00:04:57,360
Anyways, run the code.
53
00:05:02,190 --> 00:05:06,180
And sweet, changing the outside variable has no effect on the field.
54
00:05:09,040 --> 00:05:14,530
That's because the getter returns a copy of the object we're trying to access first, it indexes an
55
00:05:14,530 --> 00:05:17,860
object from the car's array, but it returns a copy of it.
56
00:05:22,120 --> 00:05:24,430
Ultimately, the letter keeps your field safe.
57
00:05:24,460 --> 00:05:29,530
Our field is now fully secure and private, there is no chance of it leaking access.
58
00:05:32,820 --> 00:05:37,740
Once again, by following these beautiful tables, we made sure the code is strong and bulletproof.
59
00:05:43,250 --> 00:05:46,040
All right, now we can remove the random variable we made.
60
00:05:49,580 --> 00:05:50,420
And that's all.
61
00:05:55,770 --> 00:06:01,380
In this lesson, we added a geter, the Geter get car returns, one car object at the index of your
62
00:06:01,380 --> 00:06:02,040
choice.
63
00:06:06,150 --> 00:06:11,250
It indexes an object from the car's array and then it returns a new copy of the object.
6455
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.