Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,520 --> 00:00:03,910
In this lesson, you will add getters to the car class.
2
00:00:05,840 --> 00:00:09,290
You might be wondering, everything is working nice and perfect.
3
00:00:09,290 --> 00:00:11,360
Why are we adding getters?
4
00:00:11,600 --> 00:00:18,800
The issue with the syntax is let's just say my intention is to just access the objects as make field.
5
00:00:18,830 --> 00:00:25,820
Well, using this same syntax, I could accidentally update this field to be Toyota when in fact it
6
00:00:25,820 --> 00:00:28,010
should have a string called Nissan.
7
00:00:28,010 --> 00:00:35,750
So having direct access to an object field is not very safe and it's considered very, very bad practice,
8
00:00:35,750 --> 00:00:41,120
which is why we have to make every single field private to the car class.
9
00:00:47,880 --> 00:00:52,840
So private means that the field is private to the class that it was defined in.
10
00:00:52,860 --> 00:00:55,180
It is not visible anywhere else.
11
00:00:55,200 --> 00:01:02,100
So if we go back to Main Java here, we're trying to directly access the make field, but the make field
12
00:01:02,100 --> 00:01:03,420
is not visible.
13
00:01:05,090 --> 00:01:08,850
Each field is private to the class that it was defined in.
14
00:01:08,870 --> 00:01:11,300
It is not visible anywhere else.
15
00:01:12,610 --> 00:01:15,880
The thing is, I still need to print the Nissan object's fields.
16
00:01:15,880 --> 00:01:19,180
So how can I get all of its field values?
17
00:01:20,020 --> 00:01:24,940
So while the fields are private, a getter is a public method.
18
00:01:25,990 --> 00:01:33,010
Whose return type depends on a particular field and it's always going to be called get followed by the
19
00:01:33,010 --> 00:01:36,520
name of your field again, using camel case.
20
00:01:38,640 --> 00:01:44,550
And what it will do is return the field of the current object that calls this method.
21
00:01:46,100 --> 00:01:48,020
Now we'll create another gutter.
22
00:01:48,050 --> 00:01:50,210
This one needs to return a double.
23
00:01:50,480 --> 00:01:52,670
It will be called Get price.
24
00:01:55,200 --> 00:02:00,540
And it will return the price field of the current object that calls this method.
25
00:02:01,880 --> 00:02:08,990
Now notice we don't need to say this make or this the price because there is no conflicting parameter.
26
00:02:10,130 --> 00:02:16,080
Java is smart enough to return the make field of the current object calls this method.
27
00:02:16,100 --> 00:02:18,230
Now we're going to need another getter.
28
00:02:18,620 --> 00:02:20,780
This one will return an integer.
29
00:02:21,540 --> 00:02:23,070
It's going to be called get.
30
00:02:23,070 --> 00:02:23,790
Yea.
31
00:02:24,720 --> 00:02:27,000
And it needs to return the year.
32
00:02:27,810 --> 00:02:30,420
Of whatever object calls this method.
33
00:02:31,810 --> 00:02:36,250
The final gutter we need to declare is one that returns a string.
34
00:02:37,500 --> 00:02:39,450
It will be called Get Color.
35
00:02:40,130 --> 00:02:46,910
And it needs to return the color field of whatever object is currently calling this method.
36
00:02:48,790 --> 00:02:50,440
So now what we have is perfect.
37
00:02:50,440 --> 00:02:56,620
Our fields are safe, sound and private, and each field has a public getter that returns a copy of
38
00:02:56,620 --> 00:02:57,400
its value.
39
00:02:57,880 --> 00:02:59,320
So now we're good to go.
40
00:03:00,340 --> 00:03:04,350
Here because we no longer have direct access to an object as field.
41
00:03:04,360 --> 00:03:06,640
What we can do is call it's getter.
42
00:03:06,640 --> 00:03:10,960
So Nissan get make Nissan get price.
43
00:03:11,980 --> 00:03:16,510
Nissan jet year and Nissan jet color.
44
00:03:16,780 --> 00:03:19,720
We can do the same thing here from the Dodge object.
45
00:03:19,720 --> 00:03:22,570
We can call get make here.
46
00:03:22,570 --> 00:03:25,960
We can call get price get year.
47
00:03:26,690 --> 00:03:28,310
And get color.
48
00:03:29,730 --> 00:03:38,070
Okay, I'm going to add breakpoints inside every getter because it's going to be really hard pressing
49
00:03:38,070 --> 00:03:42,150
the step in to button when we're already inside the print line function.
50
00:03:42,150 --> 00:03:44,400
So we'll put breakpoints here.
51
00:03:49,040 --> 00:03:52,840
So at this point, we've created two new objects of the car class.
52
00:03:52,850 --> 00:03:55,820
Each variable stores a reference to that object.
53
00:03:56,000 --> 00:03:59,120
First we call get make for this object.
54
00:04:04,200 --> 00:04:07,950
This refers to the current object that's calling this method.
55
00:04:07,950 --> 00:04:11,280
And here we return the make field of that object.
56
00:04:15,770 --> 00:04:19,430
The second getter that gets called was get price.
57
00:04:20,800 --> 00:04:24,040
Which is going to return the price field of the current object.
58
00:04:24,670 --> 00:04:30,490
The third getter that gets called is get Yea, which is going to return the year field of the current
59
00:04:30,490 --> 00:04:31,360
object.
60
00:04:35,570 --> 00:04:41,750
And the fourth getter that gets called is get Cutler, which is going to return the color field of the
61
00:04:41,750 --> 00:04:43,070
current object.
62
00:04:43,100 --> 00:04:49,160
If I press next, it prints every single field inside of the Nissan object.
63
00:04:50,250 --> 00:04:55,980
And now once again, we call get make, get price, get yea get color, but we're going to call these
64
00:04:55,980 --> 00:04:57,900
gutters for this object.
65
00:04:58,290 --> 00:04:59,430
And so once again.
66
00:05:00,560 --> 00:05:04,770
This points to the current object that's calling this method.
67
00:05:04,790 --> 00:05:07,230
That object is make is dodge.
68
00:05:07,250 --> 00:05:08,870
That's what's going to get returned.
69
00:05:08,900 --> 00:05:12,170
Here we return the price of the current object.
70
00:05:12,740 --> 00:05:15,800
Here we return the year of the current object.
71
00:05:16,340 --> 00:05:18,410
And here we return the color.
72
00:05:20,990 --> 00:05:21,790
And that's it.
73
00:05:25,170 --> 00:05:30,450
So to recap, you should always use gutters to access your objects as fields.
6632
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.