1

00:00:00,970  -->  00:00:07,500
That's not it's because final variables when that typically as a program it's a good value stored in

2

00:00:07,500  -->  00:00:09,620
variables keep changing.

3

00:00:09,640  -->  00:00:14,880
In fact the term variable itself implies that it is something whose value can be changed.

4

00:00:15,610  -->  00:00:22,090
But sometimes we may wonder why you never change that is we may want why you remain constant or fixed

5

00:00:22,300  -->  00:00:29,300
once initialized for example there may be a variable representing maximum number of courses that a student

6

00:00:29,300  -->  00:00:30,660
can register for.

7

00:00:31,040  -->  00:00:35,670
In this case you've got the maximum number of courses to be fixed.

8

00:00:35,690  -->  00:00:41,320
For this you would declare the variable as a final variable that is you would use a modifier Feiner

9

00:00:41,450  -->  00:00:48,330
in the variable declaration recall that we briefly discussed it in the switch statement Allison there

10

00:00:48,360  -->  00:00:54,570
we have to use a type of final variable called constant variable in the next lesson vividly defined

11

00:00:54,570  -->  00:00:57,200
constant variables in a more formal way.

12

00:00:57,690  -->  00:01:04,320
In fact most of the time constant variables are what we use not the final keyword can also be used with

13

00:01:04,380  -->  00:01:05,710
classes and mentors.

14

00:01:06,030  -->  00:01:08,040
But we will look at that later.

15

00:01:08,040  -->  00:01:16,190
For now let's focus only on its use with variables so the term final simply implies that something cannot

16

00:01:16,190  -->  00:01:17,250
be changed.

17

00:01:17,420  -->  00:01:22,090
That is it is final so for variables value should be final.

18

00:01:22,200  -->  00:01:26,720
That is it shouldn't change once initialized then it needs to be marked with a key word.

19

00:01:26,720  -->  00:01:28,420
Final.

20

00:01:28,630  -->  00:01:30,190
And here is an example.

21

00:01:30,290  -->  00:01:32,840
It's available pie from the math class.

22

00:01:32,920  -->  00:01:37,340
It is declared as final as it's valued £3 1:04 should never be changed.

23

00:01:39,320  -->  00:01:46,500
So fine and reliable basically implies constant if it is used with a primitive variable then the value

24

00:01:46,500  -->  00:01:47,700
can never be changed.

25

00:01:49,070  -->  00:01:50,600
But here's the interesting thing.

26

00:01:50,990  -->  00:01:55,300
If it is used with a reference variable then the reference is constant.

27

00:01:55,370  -->  00:01:57,850
There is a reference always points to the object.

28

00:01:57,890  -->  00:02:02,560
It was initialized but that is it can be reassigned another object.

29

00:02:03,020  -->  00:02:03,460
However.

30

00:02:03,470  -->  00:02:05,600
And of that object can be changed.

31

00:02:06,050  -->  00:02:07,670
So you should keep that in mind.

32

00:02:07,670  -->  00:02:12,890
Don't be under the assumption that object content cannot be changed just because it is marked does.

33

00:02:12,890  -->  00:02:20,080
Final disciplines the arts also as arts are objects so that confidence can be changed.

34

00:02:20,310  -->  00:02:28,200
But we cannot rely on a new or a good or variable find out what it will do not get a default value.

35

00:02:28,200  -->  00:02:35,040
The reason for that is a final variable gets a default value and is initialized once again.

36

00:02:35,040  -->  00:02:41,730
Let's say in a constructor using one of its parameters then we are violating its core definition.

37

00:02:41,730  -->  00:02:50,210
So when we say final we want the variables value to remain fixed forever next final key word can be

38

00:02:50,210  -->  00:02:58,190
used with any of these three variables instance local or static let's look at final instance variables

39

00:03:00,210  -->  00:03:09,140
once initialized a final instance where rebels value can not be changed for the life of the object we

40

00:03:09,140  -->  00:03:15,590
know that a final variable does not get a default value since we are talking about final instance variables

41

00:03:15,590  -->  00:03:16,190
.

42

00:03:16,190  -->  00:03:22,850
The rule is that there must be initialized either in the declaration itself or in the constructor or

43

00:03:22,850  -->  00:03:24,670
in that instance initializer.

44

00:03:25,040  -->  00:03:26,980
Otherwise we get a compiler error.

45

00:03:27,380  -->  00:03:30,050
So you can do even initializer in a method.

46

00:03:30,410  -->  00:03:35,930
That's because if you could initialize it in a method and that method is not invoked then the variable

47

00:03:35,930  -->  00:03:37,210
is never initialized.

48

00:03:37,920  -->  00:03:44,250
In that is we could very likely run into an urge when statements involving that variable gets executed

49

00:03:45,210  -->  00:03:51,670
so it needs to be initialized in one of these three places nodded If declaration LAXON an equalizer

50

00:03:51,950  -->  00:03:58,840
and is initialised later in a constructor or in an initial as a block then the final variable is referred

51

00:03:58,840  -->  00:04:09,340
to as blank 1A non-X extra a student example to include a final instance variable OK here's a student

52

00:04:09,340  -->  00:04:15,610
class and here we have the student test class and we know that regear student instances from the Student

53

00:04:15,790  -->  00:04:21,850
class and here we're creating three student instances and we're passing the state by invoking the constructor

54

00:04:22,710  -->  00:04:26,070
and the state involves the ID name and gender.

55

00:04:26,350  -->  00:04:32,830
And as you can see here the idea is being set manually by us but in real world such ideas are generated

56

00:04:32,890  -->  00:04:35,720
automatically by the system itself.

57

00:04:35,750  -->  00:04:37,820
And let's also do that in this Demel.

58

00:04:37,840  -->  00:04:40,590
OK so let's order Jondrette IDs in this demo.

59

00:04:40,960  -->  00:04:42,900
OK so two things need to happen here.

60

00:04:42,910  -->  00:04:46,770
One is the I.D. that is generated should be unique to each student.

61

00:04:47,020  -->  00:04:47,470
OK.

62

00:04:47,600  -->  00:04:52,040
And for that we will create a static variable in the Student class.

63

00:04:52,350  -->  00:04:52,800
OK.

64

00:04:52,990  -->  00:04:58,930
And the next thing is once the ID is set in an instance that value should not be changed.

65

00:04:58,930  -->  00:05:02,260
So within that object we should not be able to change the ID.

66

00:05:02,410  -->  00:05:03,770
So it needs to be.

67

00:05:04,000  -->  00:05:06,360
It needs to be fixed for the life of the object.

68

00:05:06,460  -->  00:05:09,740
And we know that we can use in final instance where the bill for that.

69

00:05:09,970  -->  00:05:13,360
OK so let's just go ahead and do that.

70

00:05:13,380  -->  00:05:15,590
Crusting Let's come on this art.

71

00:05:15,760  -->  00:05:20,400
Let me also remove this since we don't have to set it manually anymore.

72

00:05:20,650  -->  00:05:22,150
We can set it.

73

00:05:22,420  -->  00:05:23,860
It gets auto generated.

74

00:05:23,950  -->  00:05:26,500
So let's just remove this OK.

75

00:05:26,590  -->  00:05:29,320
It's going to student class fostering is let's

76

00:05:32,040  -->  00:05:35,470
change the constructor that's the parameter.

77

00:05:35,520  -->  00:05:37,060
And it's also remolded statement.

78

00:05:38,350  -->  00:05:48,760
It's going to get top let's introduce the static variable engendered the unique ID sins it's going to

79

00:05:48,760  -->  00:05:58,550
initialiser ID let's call it as ID initialiser initialize it to Dowson and here is the ID field.

80

00:05:58,960  -->  00:06:01,170
And we need to make it fun.

81

00:06:01,190  -->  00:06:03,810
So now it's a final instance OK.

82

00:06:04,100  -->  00:06:12,550
So let's just go ahead and compile this notice variable ID might not have been initialized right.

83

00:06:12,570  -->  00:06:19,320
So we said that the ID field if final variable will not get a default value and it needs to be initialized

84

00:06:19,320  -->  00:06:24,310
either in the declaration statement or in the constructor or in one of the initializer blocks.

85

00:06:24,310  -->  00:06:28,530
So since we dont have any sheilas or block so it needs to be done here or here.

86

00:06:28,560  -->  00:06:31,070
So let's just go ahead and do it here.

87

00:06:31,990  -->  00:06:41,600
Let's say ID equals plus plus ID initialiser.

88

00:06:41,970  -->  00:06:42,340
OK.

89

00:06:42,440  -->  00:06:48,190
So the first student instance will get an idea of tolls or non-blood because it's we have initialized

90

00:06:48,190  -->  00:06:54,880
it 2000 here in the top the second 2002 it's also have this pen statement.

91

00:06:54,880  -->  00:06:59,980
We are just printing the ID of the main name is basically the student mean ease.

92

00:07:00,130  -->  00:07:02,010
This is the I-T.

93

00:07:02,140  -->  00:07:03,780
So just a print statement there.

94

00:07:04,050  -->  00:07:09,920
So let's just go ahead and compile me answer on this

95

00:07:14,500  -->  00:07:21,170
as you can see for each student it's bending and ideas and ideas are generated and it's also unique

96

00:07:21,190  -->  00:07:21,410
.

97

00:07:21,570  -->  00:07:22,240
OK.

98

00:07:22,250  -->  00:07:26,530
So that's a final instance variable.

99

00:07:26,750  -->  00:07:27,370
OK.

100

00:07:27,650  -->  00:07:32,330
Now we also said that we cannot re-initialize a final variable.

101

00:07:32,330  -->  00:07:41,520
So let's just try to do that here equals cease and desist search something else it doesn't matter.

102

00:07:41,520  -->  00:07:49,110
So let's just compiler and it just cannot assign a value to final video ID.

103

00:07:49,410  -->  00:07:49,970
OK.

104

00:07:50,320  -->  00:07:56,010
So since it's final You cannot do this you can only initialize it once on once initialized.

105

00:07:56,020  -->  00:07:57,340
It's fixed.

106

00:07:57,340  -->  00:07:58,390
So that's about it.

107

00:07:58,390  -->  00:08:01,620
So that's the final instance variable.

108

00:08:02,030  -->  00:08:11,650
And let's just head back to the sleights next is find a local variables under this straightforward once

109

00:08:11,650  -->  00:08:17,920
initialized in a block that's better than the final local variables value cannot be changed for the

110

00:08:17,980  -->  00:08:19,050
rest of the block.

111

00:08:20,710  -->  00:08:23,390
Hit as an example that the method that I'm told is fine.

112

00:08:24,130  -->  00:08:31,740
So trying to change the value really to compiler next is final key word used with tactic really because

113

00:08:32,410  -->  00:08:39,330
we know the Arctic really about our blossomy rivers once initialized a final start.

114

00:08:39,340  -->  00:08:47,000
But it is constant regardless of any number of objects created for example here is a final static variable

115

00:08:47,000  -->  00:08:47,050
.

116

00:08:47,060  -->  00:08:53,310
Karl Marx underscored a value which is defined in a class called integer from the dollar doglike package

117

00:08:53,320  -->  00:08:53,620
.

118

00:08:54,430  -->  00:09:00,460
So no matter how many instances we create of this class Maximus discord you will always have this constant

119

00:09:00,460  -->  00:09:09,140
value like final instance variables final static variables must be initialized either in declaration

120

00:09:09,140  -->  00:09:12,370
or self or in the static initial as a block.

121

00:09:12,380  -->  00:09:20,530
I'd always get a compiler error not come into naming convention final static variable should be in all

122

00:09:20,530  -->  00:09:24,210
caps with an underscore separating the words.

123

00:09:24,250  -->  00:09:29,770
Here's an example from collections class which is from the Jawa got Udal package.

124

00:09:29,770  -->  00:09:35,860
So if you want to declare constant values across your entire system you can declare them as final static

125

00:09:37,010  -->  00:09:39,290
So that's an introduction to final variables.

126

00:09:39,290  -->  00:09:43,310
Next we will look at constant variables which are a type of final variables
