1
00:00:00,390 --> 00:00:07,170
Hire everyone in this tutorial I'm going to talk about class variables and class methods.

2
00:00:07,200 --> 00:00:07,860
OK.

3
00:00:07,920 --> 00:00:11,550
So first of all we need to create our Java project.

4
00:00:11,550 --> 00:00:18,660
So in order to create a Java project we need to click on File Neo and then click on Java project.

5
00:00:18,690 --> 00:00:27,710
So for the project name just type in here class variables and class methods.

6
00:00:28,200 --> 00:00:28,890
OK.

7
00:00:29,370 --> 00:00:30,600
And click on finish.

8
00:00:30,750 --> 00:00:34,000
So as you can see here our class is now created.

9
00:00:34,200 --> 00:00:39,990
And then if we need our main class for our project in order to actually execute our Java code.

10
00:00:40,020 --> 00:00:40,680
OK.

11
00:00:40,890 --> 00:00:48,240
So just click on this as our See folder and then click on File New and then click on class.

12
00:00:48,630 --> 00:00:51,780
So for the class name just type in here Main.

13
00:00:52,590 --> 00:00:55,720
And for the package we need a reverse domain here.

14
00:00:55,860 --> 00:01:03,950
So just typing here com that your name dot product name class variables

15
00:01:06,410 --> 00:01:10,720
and class methods.

16
00:01:11,250 --> 00:01:11,760
OK.

17
00:01:11,780 --> 00:01:14,690
And then for the modifiers to the public.

18
00:01:14,920 --> 00:01:15,550
OK.

19
00:01:15,560 --> 00:01:19,200
The access modifier of our class is public.

20
00:01:19,910 --> 00:01:25,040
And here make sure that you check these checkbox here public static void main method.

21
00:01:25,040 --> 00:01:25,540
OK.

22
00:01:25,700 --> 00:01:27,310
And then click on finish.

23
00:01:27,860 --> 00:01:31,010
So as you can see here our main class is now created.

24
00:01:31,190 --> 00:01:33,820
So now let me actually delete this comment here.

25
00:01:34,030 --> 00:01:34,590
OK.

26
00:01:34,730 --> 00:01:37,210
Now I want to create bad objects.

27
00:01:37,250 --> 00:01:37,970
OK.

28
00:01:37,970 --> 00:01:41,560
So first of all you need a class called the bird.

29
00:01:41,750 --> 00:01:48,980
So in order to create a class just click on this package here and then click on File New and then click

30
00:01:48,980 --> 00:01:50,130
on a class.

31
00:01:50,270 --> 00:01:52,320
So for the class name just type in here.

32
00:01:53,860 --> 00:01:54,640
OK.

33
00:01:54,950 --> 00:01:57,370
And for the modifier this is public.

34
00:01:57,560 --> 00:02:05,260
And then click on finish so as you can see here our class called Baird is now created.

35
00:02:05,280 --> 00:02:12,720
So now inside of this class I want to put some instructions in order to show me how to create birth

36
00:02:12,720 --> 00:02:13,840
objects.

37
00:02:13,860 --> 00:02:20,910
So first of all I want to create an instance variable for my birth objects so the data type of my instance

38
00:02:20,910 --> 00:02:26,730
variable is it and the name of my instance variable is speed.

39
00:02:27,390 --> 00:02:29,620
And I want to assign a value to my body.

40
00:02:29,640 --> 00:02:31,600
For example 100 than 80.

41
00:02:31,940 --> 00:02:32,680
OK.

42
00:02:33,270 --> 00:02:40,950
And remember that I told you in the previous tutorial that you should always use this private access

43
00:02:40,950 --> 00:02:48,690
modifier for your instance variables in order not to allow any other classes other than disparate class

44
00:02:48,930 --> 00:02:51,900
to access this spit instance variable directory.

45
00:02:51,900 --> 00:02:56,900
OK so now I want to create a getter my friend and I set her my faith for my speed.

46
00:02:56,900 --> 00:02:58,270
Instance variable.

47
00:02:58,590 --> 00:03:03,550
So in order to create or get her in my head I just need to type in here public.

48
00:03:04,000 --> 00:03:04,550
OK.

49
00:03:04,680 --> 00:03:08,480
The access modifier of my get her method is public.

50
00:03:08,490 --> 00:03:09,230
OK.

51
00:03:09,540 --> 00:03:16,850
And the return type of mine if it is in it and the name of my method is get speed.

52
00:03:17,850 --> 00:03:21,970
And then I need to put in a pair of round brackets after my method name.

53
00:03:22,170 --> 00:03:26,460
And then I need to open and close my method by using these curly braces.

54
00:03:26,460 --> 00:03:26,850
OK.

55
00:03:26,850 --> 00:03:33,060
So inside of this nymphet I just need to return the value of my speed instance value of it.

56
00:03:33,090 --> 00:03:36,260
So here I just typed in return speed.

57
00:03:36,310 --> 00:03:37,030
OK.

58
00:03:37,650 --> 00:03:43,940
So now I want to actually create or set her my fed for my speed instance variable.

59
00:03:43,950 --> 00:03:53,280
So again the access modifier of my method is public and the return type of my Meffert is void and the

60
00:03:53,280 --> 00:03:56,690
name of my method is set speed.

61
00:03:57,060 --> 00:04:03,600
And then I need to put in a pair of round brackets after my Meffert name and this time I want to specify

62
00:04:03,870 --> 00:04:06,750
that my method is going to accept an argument.

63
00:04:06,990 --> 00:04:12,620
So the data type of my argument is int and the name of my argument is value.

64
00:04:13,020 --> 00:04:18,110
And then I need to open and close my method by using these curly braces.

65
00:04:18,180 --> 00:04:25,750
So here I want to actually assign the value of my argument to my speed instance variable.

66
00:04:25,890 --> 00:04:29,940
So first I need to refer to the name of my speed instance variable.

67
00:04:30,300 --> 00:04:35,830
And then I put an assignment operator here and then I refer to the name of my argument.

68
00:04:36,120 --> 00:04:45,930
OK so now I might get her method and my certain method for this instance variable is created.

69
00:04:46,050 --> 00:04:50,840
So now I want to actually create a behavior for my birth objects.

70
00:04:50,850 --> 00:04:55,060
So again the access modifier of my method is public.

71
00:04:55,140 --> 00:05:00,780
So for the behaviors of your objects always use the public access modifier.

72
00:05:00,780 --> 00:05:01,540
OK.

73
00:05:01,650 --> 00:05:07,560
Or you can actually use the package access modifier if you want to only allow the classes inside of

74
00:05:07,560 --> 00:05:10,200
this package to use that behavior.

75
00:05:10,200 --> 00:05:10,610
OK.

76
00:05:10,620 --> 00:05:15,720
So in order to actually use the package access modifier you don't need to write any key word here.

77
00:05:15,720 --> 00:05:16,310
OK.

78
00:05:16,500 --> 00:05:22,930
The package access modifier is the default access modifier for all of your variables and methods.

79
00:05:23,010 --> 00:05:25,470
So the return type of My method is void.

80
00:05:25,900 --> 00:05:28,780
And for example my bird can fly.

81
00:05:28,840 --> 00:05:35,740
OK so the name of my method is fly and then I need to put in a pair of round brackets after my name

82
00:05:36,060 --> 00:05:42,420
and then I need to open and close my fit by using these curly braces and inside of this if I can't put

83
00:05:42,420 --> 00:05:43,920
my statements.

84
00:05:43,920 --> 00:05:50,710
So here I just need to type in system that's out that insulin.

85
00:05:50,890 --> 00:05:56,620
And then inside the prentices I just put it into double quotes here and then inside of these double

86
00:05:56,620 --> 00:05:59,110
quotes I can just type in fly.

87
00:05:59,230 --> 00:06:03,150
And then I need to put in a semi-colon at the end of my statement.

88
00:06:03,190 --> 00:06:06,590
So as you can see here our blueprint is now ready.

89
00:06:06,880 --> 00:06:11,160
So now we want to create weird objects from disparate class.

90
00:06:11,170 --> 00:06:11,700
OK.

91
00:06:11,740 --> 00:06:17,320
So inside of this main class here I just need to double click on this means that we have here in order

92
00:06:17,320 --> 00:06:20,230
to make this a little earlier or much wider.

93
00:06:20,260 --> 00:06:24,310
So now we want to create objects from this birth class.

94
00:06:24,310 --> 00:06:32,870
So first of all I need to refer to the name of my class bird and the name of my object is my beard and

95
00:06:32,860 --> 00:06:35,200
that I put an assignment operator here.

96
00:06:35,470 --> 00:06:38,000
And then I use this key word here new.

97
00:06:38,560 --> 00:06:43,580
And then again you refer to the name of my class and then I put in a pair of round brackets.

98
00:06:43,720 --> 00:06:44,800
After my class name.

99
00:06:44,880 --> 00:06:50,210
OK so now I can actually access the characteristics or behaviors of my birth object.

100
00:06:50,230 --> 00:06:50,750
OK.

101
00:06:50,950 --> 00:06:57,030
So for example I want to access the value of the speed instance variable and then outputs that value

102
00:06:57,040 --> 00:06:57,840
to the console.

103
00:06:58,000 --> 00:07:05,650
So I just to type in here system that out that insulin and it inside of the prentices.

104
00:07:05,650 --> 00:07:12,650
First I need to refer to the name of my object my bird that gets speed OK.

105
00:07:12,790 --> 00:07:16,260
And then I need to put in a semicolon at the end of my statement.

106
00:07:16,270 --> 00:07:21,760
So now if I run my project as you can see this is the value of the speed instance variable.

107
00:07:21,760 --> 00:07:22,290
OK.

108
00:07:22,450 --> 00:07:29,690
Or I can actually tell my birth object to fly my birth dot fly.

109
00:07:30,030 --> 00:07:30,580
OK.

110
00:07:30,760 --> 00:07:35,820
And now if I run my project as you can see this value is outputted to the console.

111
00:07:35,830 --> 00:07:44,190
OK so now I going to actually talk about class variables and class methods.

112
00:07:44,230 --> 00:07:49,930
Class variables are variables defined and stored in the class itself.

113
00:07:49,930 --> 00:07:57,380
Their values apply to the class and all its instances with instance variables.

114
00:07:57,400 --> 00:08:05,500
Each new instance of the class gets a new copy of the instance variables that the class defines each

115
00:08:05,500 --> 00:08:13,570
instance then can change the values of those instance variables without affecting any other instances.

116
00:08:13,570 --> 00:08:17,600
OK I actually showed you some of those examples in the previous tutorials.

117
00:08:17,650 --> 00:08:26,470
I told you that every object actually gets a new copy of the instance valuable that we define in that

118
00:08:26,470 --> 00:08:27,360
specific class.

119
00:08:27,370 --> 00:08:27,900
OK.

120
00:08:28,090 --> 00:08:36,130
So for example if I change the value of the speed instance valuable for this my birth object we are

121
00:08:36,130 --> 00:08:40,500
actually changing a copy of that instance variable.

122
00:08:40,510 --> 00:08:41,000
OK.

123
00:08:41,050 --> 00:08:47,050
And if I create another object from this class the value of that instance variable is going to be unchanged.

124
00:08:47,080 --> 00:08:47,730
OK.

125
00:08:47,830 --> 00:08:52,690
Because we are only creating a copy for our object.

126
00:08:52,690 --> 00:08:55,510
For every object that we create from this class.

127
00:08:55,570 --> 00:09:04,000
Ok this is for instance variables but with class variables is only one copy of that variable exists.

128
00:09:04,000 --> 00:09:11,920
When the class is loaded changing the value of that variable changes it for all instances of that class

129
00:09:11,930 --> 00:09:18,180
redefined class variables by including the static keyword before the value itself.

130
00:09:18,290 --> 00:09:23,590
OK so now let me actually show you some real examples in order to make this concept more meaningful

131
00:09:23,830 --> 00:09:24,730
for you.

132
00:09:24,730 --> 00:09:27,740
So first let me actually comment out these lines of code here.

133
00:09:27,850 --> 00:09:28,560
OK.

134
00:09:28,870 --> 00:09:35,320
And now inside the bird's class here and now inside the parent class here.

135
00:09:35,350 --> 00:09:39,830
Firstly actually change this private access modifier to public.

136
00:09:40,380 --> 00:09:40,950
OK.

137
00:09:41,170 --> 00:09:47,710
And then we need to put this tatic vote here after our access modifier in order to make this instance

138
00:09:47,710 --> 00:09:49,400
valuable a class variable.

139
00:09:49,420 --> 00:09:50,010
OK.

140
00:09:50,010 --> 00:09:50,530
Static.

