1 00:00:01,700 --> 00:00:03,890 Static variables belong to a class. 2 00:00:08,530 --> 00:00:12,610 Fields belong to an object, but static variables belong to the class. 3 00:00:17,510 --> 00:00:20,060 In this lesson, you're going to learn about static variables. 4 00:00:24,080 --> 00:00:29,390 First things first in your Java Bootcamp resources, open up the folder for this lesson by following 5 00:00:29,390 --> 00:00:30,110 this path. 6 00:00:38,140 --> 00:00:43,870 All right, a static variable, as we just said, belongs to the class, a static variable does not 7 00:00:43,870 --> 00:00:45,280 belong to an object. 8 00:00:49,830 --> 00:00:53,700 The employee class models to fields and has one static variable. 9 00:00:54,750 --> 00:01:00,290 Each employee object that we create from the employee class has different values for name and position. 10 00:01:01,810 --> 00:01:06,580 But the number of employees hired to relate to the class, not objects of the class. 11 00:01:14,490 --> 00:01:17,910 Inside main Java create three objects of the employee class. 12 00:01:23,630 --> 00:01:26,330 I'll create an employee called Zack, new employee. 13 00:01:32,050 --> 00:01:33,670 We'll make him a programmer. 14 00:01:43,330 --> 00:01:44,960 Or create another one named Geo. 15 00:01:48,200 --> 00:01:49,040 Analyst. 16 00:01:51,840 --> 00:01:53,700 And let's say Thomas. 17 00:01:55,910 --> 00:01:56,930 Specialist. 18 00:02:00,450 --> 00:02:05,490 All right, your task is to count how many employees were hired, the number of employees hired relates 19 00:02:05,490 --> 00:02:07,890 to the class, not objects of the class. 20 00:02:13,200 --> 00:02:17,670 So inside employee class create a static integer variable called hired count. 21 00:02:21,750 --> 00:02:26,610 Every time we create a new object of the employee class, we're going to update hired count by one. 22 00:02:33,850 --> 00:02:38,680 And because a static variable belongs to the class, you can access it directly from the class type. 23 00:02:41,660 --> 00:02:45,710 We're going to print the number of employees we hired employee hired count. 24 00:02:50,400 --> 00:02:51,210 Run the code. 25 00:02:58,550 --> 00:03:00,480 And the number of employees hired is three. 26 00:03:01,370 --> 00:03:07,310 The output is more interesting with breakpoints, I'm out of Breakpoint next to each object launched 27 00:03:07,310 --> 00:03:07,940 the debugger. 28 00:03:12,780 --> 00:03:15,210 I'll step into the constructor for the first object. 29 00:03:30,390 --> 00:03:32,760 The constructor updates every field in the object. 30 00:03:36,090 --> 00:03:37,710 And it updates the static variable. 31 00:03:39,370 --> 00:03:45,460 Notice that the static variable is not a member of the object the Zach object defines to Field's name 32 00:03:45,460 --> 00:03:48,560 and position, the static variable belongs to the class. 33 00:03:48,730 --> 00:03:50,320 And right now, it equals one. 34 00:03:53,930 --> 00:03:58,610 I'll step into the constructor for the constructor, updates every field in the object. 35 00:04:00,640 --> 00:04:02,590 And it updates the static variable to two. 36 00:04:05,790 --> 00:04:11,960 The Jobs Act defines Namen position, but the static variable which belongs to the class now equals 37 00:04:11,970 --> 00:04:12,270 to. 38 00:04:18,240 --> 00:04:20,339 I'll step into the constructor for Thomas. 39 00:04:24,970 --> 00:04:26,800 The constructor updates every field. 40 00:04:29,030 --> 00:04:31,040 And it updates the static variable to three. 41 00:04:35,270 --> 00:04:38,030 Each object defines a unique name and position. 42 00:04:40,270 --> 00:04:45,610 But hired count is a member of the employee class, and that equals three. 43 00:04:52,220 --> 00:04:57,860 Let's recap, a static variable belongs to the class, in other words, a static variable relates to 44 00:04:57,860 --> 00:05:00,590 the class, not objects of the class.