Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,690 --> 00:00:02,969
Static methods belong to the class.
2
00:00:06,230 --> 00:00:10,780
Methods belong to the object, but static methods belong directly to the class.
3
00:00:13,620 --> 00:00:16,470
In this lesson, you will learn to create static methods.
4
00:00:20,950 --> 00:00:23,720
First thing's first hired count needs to be private.
5
00:00:24,130 --> 00:00:27,430
The only place that needs to update hired count is the constructor.
6
00:00:27,820 --> 00:00:30,720
The caller shouldn't be allowed to update it from Maine.
7
00:00:31,000 --> 00:00:32,650
This is a security risk.
8
00:00:47,650 --> 00:00:49,360
So make it private.
9
00:00:58,710 --> 00:01:05,160
Now, a normal method belongs to an object because methods belong to an object, you can only call them
10
00:01:05,160 --> 00:01:08,130
from the object like geotag get name.
11
00:01:20,000 --> 00:01:26,240
Now create a method named get hired, counts public and get hired counts and it will return the number
12
00:01:26,240 --> 00:01:27,500
of employees we hired.
13
00:01:31,100 --> 00:01:36,440
Do you see what's wrong with the code, the static variable belongs to the class, it does not belong
14
00:01:36,470 --> 00:01:38,030
to a particular object.
15
00:01:38,390 --> 00:01:43,730
But in order to call get hired count, you need to create an object of the employee class.
16
00:01:47,070 --> 00:01:52,710
In this case, I would have to create a new object of the employee class passing some random values
17
00:01:52,890 --> 00:01:55,230
for the purpose of calling get hired counts.
18
00:01:58,190 --> 00:02:04,180
All right, with that being said, accessing a static variable from an object doesn't make any sense.
19
00:02:09,169 --> 00:02:12,230
You should be able to call this method directly from the class.
20
00:02:13,730 --> 00:02:15,890
A static method belongs to the class.
21
00:02:17,240 --> 00:02:19,670
So you can call it directly from the class.
22
00:02:23,700 --> 00:02:26,550
Inside the employee class make the method static.
23
00:02:32,810 --> 00:02:35,120
And now we can call the method directly from the class.
24
00:02:36,150 --> 00:02:38,520
Java is even telling us to call it from the class.
25
00:02:43,310 --> 00:02:45,440
Employees don't get hired counts.
26
00:02:51,530 --> 00:02:52,460
And perfect.
27
00:02:55,860 --> 00:02:58,690
We're able to access the static variable directly from the class.
28
00:02:59,010 --> 00:03:04,110
It's also private, which means we don't have to worry about accidental updates inside men.
29
00:03:08,080 --> 00:03:14,110
That being said, a static method can only access static variables, I'm going to repeat this one more
30
00:03:14,110 --> 00:03:14,470
time.
31
00:03:14,780 --> 00:03:18,130
A static method can only access static variables.
32
00:03:23,630 --> 00:03:29,300
This begs the question, why can't a static method access fields paused the video and think about it.
33
00:03:35,940 --> 00:03:40,770
The answer is a static method belongs to a class and fields belong to an object.
34
00:03:40,800 --> 00:03:41,910
It's as simple as that.
35
00:03:50,050 --> 00:03:53,140
Let's say you tried to access this name from a static method.
36
00:03:56,870 --> 00:03:59,220
For clarity, I'm just going to use that this keyword.
37
00:03:59,240 --> 00:04:04,910
It's the same thing, but anyways, this points to the current object that's calling this method.
38
00:04:22,240 --> 00:04:28,060
So there's no object for this to point to, which is why job is going to tell you you cannot access
39
00:04:28,060 --> 00:04:30,520
in objects fields from a static context.
40
00:04:37,900 --> 00:04:45,010
Also, a static method can only access other static methods, take some time to really internalize the
41
00:04:45,010 --> 00:04:46,600
following two sentences.
42
00:04:54,840 --> 00:05:00,150
OK, why can't a static method access normal methods, pause the video and think about it?
43
00:05:05,670 --> 00:05:11,580
The answer is simple a static method belongs to a class and a normal method belongs to an object.
44
00:05:14,690 --> 00:05:17,990
Let's say you tried to access this thought, get name from a static method.
45
00:05:22,790 --> 00:05:28,970
This points to the current object calling get name, but you're not calling get hired count from an
46
00:05:28,970 --> 00:05:31,300
object, you're calling it from a class.
47
00:05:31,460 --> 00:05:34,130
So there is no object for this to point to.
48
00:05:34,730 --> 00:05:38,750
Job is going to tell you you can't access normal methods from a static context.
49
00:05:38,750 --> 00:05:40,060
It doesn't make any sense.
50
00:05:50,970 --> 00:05:57,180
So you understand these rules and you should be fine, a static method can only access static variables
51
00:05:57,360 --> 00:06:00,420
and it can only access other static methods.
52
00:06:04,210 --> 00:06:05,210
Here's a question for you.
53
00:06:05,350 --> 00:06:08,020
Why does everything in Maine have to be static?
54
00:06:12,150 --> 00:06:15,810
It doesn't make any sense to create an object of the main class.
55
00:06:19,380 --> 00:06:24,300
For example, the main method is the entry point of your application, the compiler wants to call the
56
00:06:24,300 --> 00:06:28,680
main method, but it doesn't want to have to create an object of the main class to do so.
57
00:06:29,010 --> 00:06:32,390
It just wants to call it from a static context for simplicity.
58
00:06:32,400 --> 00:06:34,470
It just wants to say mean that mean.
59
00:06:39,460 --> 00:06:42,460
All right, why do class variables in Maine need to be static?
60
00:06:42,790 --> 00:06:48,130
Remember, the first part of our rule, a static method can only access static variables.
61
00:06:49,850 --> 00:06:53,080
If I want to use scanner inside mean it needs to be static as well.
62
00:07:05,620 --> 00:07:08,310
If I remove the static key word, it's not going to work.
63
00:07:15,600 --> 00:07:21,660
Why do other methods and may need to be static, remember the second part of our role, a static method,
64
00:07:21,660 --> 00:07:24,090
can only access other static methods.
65
00:07:27,000 --> 00:07:31,050
I'm going to create a random void method in Maine, public void, do stuff.
66
00:07:37,590 --> 00:07:40,610
I'll try to call it from the main method, and it's not going to work.
67
00:07:45,370 --> 00:07:48,870
If I want to call this method for Maine, it needs to be static as well.
68
00:07:55,760 --> 00:07:58,940
Let's recap in this lesson, you learn to create static methods.
69
00:08:00,130 --> 00:08:04,600
Static methods belong to the class, so you can call them directly from the class type.
70
00:08:07,490 --> 00:08:12,890
And there are two things you should know about static methods a static method can only access static
71
00:08:12,890 --> 00:08:17,630
variables and a static method can only access other static methods.
7236
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.