Static methods belong to the class.
Methods belong to the object, but static methods belong directly to the class.
In this lesson, you will learn to create static methods.
First thing's first hired count needs to be private.
The only place that needs to update hired count is the constructor.
The caller shouldn't be allowed to update it from Maine.
This is a security risk.
So make it private.
Now, a normal method belongs to an object because methods belong to an object, you can only call them
from the object like geotag get name.
Now create a method named get hired, counts public and get hired counts and it will return the number
of employees we hired.
Do you see what's wrong with the code, the static variable belongs to the class, it does not belong
to a particular object.
But in order to call get hired count, you need to create an object of the employee class.
In this case, I would have to create a new object of the employee class passing some random values
for the purpose of calling get hired counts.
All right, with that being said, accessing a static variable from an object doesn't make any sense.
You should be able to call this method directly from the class.
A static method belongs to the class.
So you can call it directly from the class.
Inside the employee class make the method static.
And now we can call the method directly from the class.
Java is even telling us to call it from the class.
Employees don't get hired counts.
And perfect.
We're able to access the static variable directly from the class.
It's also private, which means we don't have to worry about accidental updates inside men.
That being said, a static method can only access static variables, I'm going to repeat this one more
time.
A static method can only access static variables.
This begs the question, why can't a static method access fields paused the video and think about it.
The answer is a static method belongs to a class and fields belong to an object.
It's as simple as that.
Let's say you tried to access this name from a static method.
For clarity, I'm just going to use that this keyword.
It's the same thing, but anyways, this points to the current object that's calling this method.
So there's no object for this to point to, which is why job is going to tell you you cannot access
in objects fields from a static context.
Also, a static method can only access other static methods, take some time to really internalize the
following two sentences.
OK, why can't a static method access normal methods, pause the video and think about it?
The answer is simple a static method belongs to a class and a normal method belongs to an object.
Let's say you tried to access this thought, get name from a static method.
This points to the current object calling get name, but you're not calling get hired count from an
object, you're calling it from a class.
So there is no object for this to point to.
Job is going to tell you you can't access normal methods from a static context.
It doesn't make any sense.
So you understand these rules and you should be fine, a static method can only access static variables
and it can only access other static methods.
Here's a question for you.
Why does everything in Maine have to be static?
It doesn't make any sense to create an object of the main class.
For example, the main method is the entry point of your application, the compiler wants to call the
main method, but it doesn't want to have to create an object of the main class to do so.
It just wants to call it from a static context for simplicity.
It just wants to say mean that mean.
All right, why do class variables in Maine need to be static?
Remember, the first part of our rule, a static method can only access static variables.
If I want to use scanner inside mean it needs to be static as well.
If I remove the static key word, it's not going to work.
Why do other methods and may need to be static, remember the second part of our role, a static method,
can only access other static methods.
I'm going to create a random void method in Maine, public void, do stuff.
I'll try to call it from the main method, and it's not going to work.
If I want to call this method for Maine, it needs to be static as well.
Let's recap in this lesson, you learn to create static methods.
Static methods belong to the class, so you can call them directly from the class type.
And there are two things you should know about static methods a static method can only access static
variables and a static method can only access other static methods.