Static variables belong to a class.
Fields belong to an object, but static variables belong to the class.
In this lesson, you're going to learn about static variables.
First things first in your Java Bootcamp resources, open up the folder for this lesson by following
this path.
All right, a static variable, as we just said, belongs to the class, a static variable does not
belong to an object.
The employee class models to fields and has one static variable.
Each employee object that we create from the employee class has different values for name and position.
But the number of employees hired to relate to the class, not objects of the class.
Inside main Java create three objects of the employee class.
I'll create an employee called Zack, new employee.
We'll make him a programmer.
Or create another one named Geo.
Analyst.
And let's say Thomas.
Specialist.
All right, your task is to count how many employees were hired, the number of employees hired relates
to the class, not objects of the class.
So inside employee class create a static integer variable called hired count.
Every time we create a new object of the employee class, we're going to update hired count by one.
And because a static variable belongs to the class, you can access it directly from the class type.
We're going to print the number of employees we hired employee hired count.
Run the code.
And the number of employees hired is three.
The output is more interesting with breakpoints, I'm out of Breakpoint next to each object launched
the debugger.
I'll step into the constructor for the first object.
The constructor updates every field in the object.
And it updates the static variable.
Notice that the static variable is not a member of the object the Zach object defines to Field's name
and position, the static variable belongs to the class.
And right now, it equals one.
I'll step into the constructor for the constructor, updates every field in the object.
And it updates the static variable to two.
The Jobs Act defines Namen position, but the static variable which belongs to the class now equals
to.
I'll step into the constructor for Thomas.
The constructor updates every field.
And it updates the static variable to three.
Each object defines a unique name and position.
But hired count is a member of the employee class, and that equals three.
Let's recap, a static variable belongs to the class, in other words, a static variable relates to
the class, not objects of the class.