1

00:00:01,060  -->  00:00:07,160
Not let's look at constant variables we briefly look at them while discussing such statements.

2

00:00:07,170  -->  00:00:10,210
Let's not look at them in a more formal way.

3

00:00:10,250  -->  00:00:18,030
We know that variables are evaluated at runtime or not compile time Hollywood constant variables are

4

00:00:18,030  -->  00:00:23,820
a kind of final variables whose values are known at compile time X and that is we don't have to wait

5

00:00:23,850  -->  00:00:26,320
until runtime to know their values.

6

00:00:26,820  -->  00:00:32,870
For this reason they're usually also referred to as compile time constants.

7

00:00:33,000  -->  00:00:34,710
And here is one example.

8

00:00:34,710  -->  00:00:38,350
It is variable pipe from the math class.

9

00:00:38,940  -->  00:00:44,940
The benefit of knowing the value at compile time is that the compiler can take that value and replace

10

00:00:45,060  -->  00:00:48,690
every instance of the variable with the actual value itself.

11

00:00:49,290  -->  00:00:55,100
And this element in some runtime or heard for example here we have the variable x which is assigned

12

00:00:55,110  -->  00:01:01,890
my doc by but my doc is a compile time constraint and the compiler knows the corresponding value 3.1

13

00:01:01,890  -->  00:01:09,910
for services 3.1 for directly to the variable x and store it in the class file as part of the bike.

14

00:01:10,620  -->  00:01:16,200
So this Elements need some overhead at runtime as Debian doesn't have to look into the math class to

15

00:01:16,200  -->  00:01:17,620
make this assignment.

16

00:01:18,090  -->  00:01:21,940
So compiler is able to perform such an optimization.

17

00:01:22,950  -->  00:01:30,060
So basically constant Fribbles are final variables but for a final variable to be qualified as a constant

18

00:01:30,060  -->  00:01:34,880
variable there are few restrictions on that look at them.

19

00:01:35,370  -->  00:01:40,160
First is obviously it needs to be declared with a modifier final.

20

00:01:40,710  -->  00:01:45,420
Next did different type of variable should be either a Plimmer or a string.

21

00:01:45,420  -->  00:01:50,590
If you have any other the debate then it will not be a constant variable.

22

00:01:50,940  -->  00:01:54,860
Next a variable must be initialized in the declaration statement itself.

23

00:01:55,380  -->  00:02:00,690
That is it cannot be initialized somewhere else like in a constructor or in some initial as a block

24

00:02:00,700  -->  00:02:01,660
.

25

00:02:02,610  -->  00:02:07,500
Finally the initialization expression itself which is on the right side of the assignment must be a

26

00:02:07,500  -->  00:02:14,340
constant expression a constant expression is something whose value is known at compile time itself and

27

00:02:14,340  -->  00:02:18,900
hence it is also referred to as compile time constant expression.

28

00:02:18,900  -->  00:02:23,780
There are some rules on glauca expressions qualify as constant expressions.

29

00:02:23,940  -->  00:02:27,100
You can check out the resources section for full list of those.

30

00:02:27,570  -->  00:02:32,610
But let's look at a few examples now that used the most commonly used rules.

31

00:02:32,700  -->  00:02:38,340
Typically you would see either Plim Dolittle's or string locos as constant expressions like in the case

32

00:02:38,340  -->  00:02:40,620
of my doc.

33

00:02:41,310  -->  00:02:47,760
Here is a simple example of a constant variable the constant expression is simply an intersection.

34

00:02:48,330  -->  00:02:53,170
In this case we have a string literal as a constant expression.

35

00:02:53,190  -->  00:02:58,290
This is also a constant expression as a value will be known at compile time itself.

36

00:02:59,820  -->  00:03:05,460
This is also a valid constraint expression as a final concatenated string is known at compile time itself

37

00:03:05,460  -->  00:03:07,160
.

38

00:03:07,170  -->  00:03:13,920
Finally here in the second declaration statement the variable x would be a Garlick constant variable

39

00:03:14,370  -->  00:03:21,540
as a variable z itself is a constant variable and hence its value is known at compile time and so the

40

00:03:21,540  -->  00:03:27,170
compiler replaces the variable z with the value for you.

41

00:03:27,210  -->  00:03:29,500
Now here is the same example we just saw.

42

00:03:29,760  -->  00:03:36,840
But here Z is not a constant variable as there is no final modifier in its declaration and so X is also

43

00:03:36,900  -->  00:03:40,370
not a constant variable.

44

00:03:40,380  -->  00:03:47,700
Here x is not a constant variable as that get to all matter will only be invoked at runtime.

45

00:03:47,700  -->  00:03:53,910
Here you are declaring a final variable x but it will not be a constant variable as initialization is

46

00:03:53,910  -->  00:04:00,450
not being done in the declaration statement itself initialization is being done in the constructor and

47

00:04:00,450  -->  00:04:05,030
the constructor of code is run only at runtime when an object is created.

48

00:04:05,310  -->  00:04:09,270
Not that objects are created on the Ekron bank.

49

00:04:10,010  -->  00:04:12,090
Here is a similar example here.

50

00:04:12,090  -->  00:04:19,590
Variable x is bootstrap big and fine but is not a constant variable as initialization is being done

51

00:04:19,590  -->  00:04:24,680
in a static initializer block or not in the declaration statement.

52

00:04:24,870  -->  00:04:30,660
Not that the static initializer block will be run only at runtime when this class has access for the

53

00:04:30,660  -->  00:04:33,650
very first time at runtime.

54

00:04:33,690  -->  00:04:36,110
Then this class is access for the very first time.

55

00:04:36,230  -->  00:04:43,050
Let's say by creating an instance of it or by invoking one off its static matter then JVM just loads

56

00:04:43,050  -->  00:04:47,700
the class into memory and then it executes this tactic initialize that block.

57

00:04:47,970  -->  00:04:54,120
At which time the variable is initialized we will deal with this class loading process in JVM in Genesis

58

00:04:54,120  -->  00:05:02,240
chapter that's the end of our discussion on final variables or not let's look at a quick demo of constant

59

00:05:02,250  -->  00:05:05,030
variables before turning them on.

60

00:05:05,060  -->  00:05:07,400
I just want to make a few closing comments.

61

00:05:07,860  -->  00:05:11,850
Typically you will see final variables as constant variables.

62

00:05:11,970  -->  00:05:18,840
That is it may not be that common to encounter final variables that object references more of what you

63

00:05:18,830  -->  00:05:20,640
will see them as constants.

64

00:05:20,630  -->  00:05:27,440
Tactic redeemers there is constant variables but that static modifier like in the case of mad dog bite

65

00:05:28,950  -->  00:05:34,290
in general if the variables are valuable not change you can liberally use the final modifier.

66

00:05:34,740  -->  00:05:41,110
Some programmers tend to use final modifier a lot with local variables like method parameters.

67

00:05:41,150  -->  00:05:46,650
It is fine but some programmers also feel that it is too verbose and clutters the code.

68

00:05:46,940  -->  00:05:52,920
Although there can be benefit of some minor compiler optimizations like we discussed earlier so it can

69

00:05:52,920  -->  00:05:55,260
be your personal choice.

70

00:05:55,250  -->  00:06:01,670
One suggestion is you may want to consider using Final bit local variables only if it is some special

71

00:06:01,680  -->  00:06:06,670
kind of construct and it would be worthwhile to highlight it that way.

72

00:06:06,890  -->  00:06:12,710
You can also check out the resources section for some really good pointers on final variables.

73

00:06:12,760  -->  00:06:15,040
No need to look at them.

74

00:06:16,380  -->  00:06:20,370
OK in this demo we are going to use the Java compiler a few times.

75

00:06:20,630  -->  00:06:26,060
So in the interest of time I'm going to show up on my Eclipse ID as it will save us some time due to

76

00:06:26,060  -->  00:06:28,340
the number of completions involved.

77

00:06:28,520  -->  00:06:31,140
So eclipse will help us save some time there.

78

00:06:31,230  -->  00:06:37,070
So for this demo I'm just going to use the same switch example that we have used the example that we

79

00:06:37,070  -->  00:06:39,340
have seen in our switch statement.

80

00:06:39,570  -->  00:06:40,180
OK.

81

00:06:40,190  -->  00:06:47,150
And the reason I'm doing that is because for if you recall for the switch statement the case label here

82

00:06:47,150  -->  00:06:54,830
has to be a constant expression and a constant expression can be an integer or a string return or it

83

00:06:54,890  -->  00:07:00,150
needs to be a constant variable so that just within the context of the switch statement.

84

00:07:00,300  -->  00:07:07,080
Ok so since it can since it can be a constant variable here and that's what we are testing in this demo

85

00:07:07,090  -->  00:07:07,360
.

86

00:07:07,640  -->  00:07:10,020
So we are just picking this example here.

87

00:07:10,110  -->  00:07:11,780
OK so let's just.

88

00:07:11,850  -->  00:07:13,580
So this is a switch example if you recall.

89

00:07:13,590  -->  00:07:19,940
This is the month variable which is initialized with 308 or is byte here and it gets matched to one

90

00:07:19,940  -->  00:07:20,960
of these labels.

91

00:07:21,000  -->  00:07:22,650
Unprinted that property value.

92

00:07:22,910  -->  00:07:27,820
OK so let's just use this exact same example that we have seen and

93

00:07:30,720  -->  00:07:38,910
our switch DeMaio and we defined a variable called to and it's initialized to 2 and it gives a compiler

94

00:07:38,900  -->  00:07:40,940
error because it's not a constant expression.

95

00:07:40,940  -->  00:07:43,340
This gets evaluated only at runtime.

96

00:07:43,360  -->  00:07:47,570
Again you may recall lot so this needs to be a constant variable and conduct.

97

00:07:47,610  -->  00:07:56,000
If you recall now it's a constant variable because it is final and importantly it's also initialized

98

00:07:56,000  -->  00:07:58,910
here in the declaration statement itself.

99

00:07:59,310  -->  00:08:00,760
Now it's a constant variable.

100

00:08:00,770  -->  00:08:06,940
Now let's just move this outside off the switch statement of the of the matter.

101

00:08:07,130  -->  00:08:08,890
So let's just put it here.

102

00:08:09,360  -->  00:08:10,380
No this will not work.

103

00:08:10,410  -->  00:08:16,430
And that's because this is an instance where it will know what the method is static and if you recon

104

00:08:16,500  -->  00:08:22,530
from static method it cannot access instance variables because static methods do not have anything to

105

00:08:22,520  -->  00:08:23,420
do with state.

106

00:08:23,630  -->  00:08:27,820
So simple just make it static here.

107

00:08:27,840  -->  00:08:28,640
Now we are good.

108

00:08:28,690  -->  00:08:32,940
OK now we are good to compile time constant.

109

00:08:33,600  -->  00:08:40,500
OK now let's remove that and let's initialize it here.

110

00:08:40,650  -->  00:08:44,100
Dieu equal do.

111

00:08:44,340  -->  00:08:54,680
Actually it needs to be a static started block and it's a Mundt do the quiz do.

112

00:08:54,750  -->  00:08:58,880
Once again this is not going to work because it's not a compile time constant.

113

00:08:58,880  -->  00:09:05,420
It's not a constant variable and if the reason for that as we discussed the initialization is being

114

00:09:05,420  -->  00:09:09,110
done in the static initializer block or not in the declaration statement.

115

00:09:09,340  -->  00:09:15,290
OK and this block will be done only at runtime when this class is loaded into entombment Evangelium

116

00:09:15,300  -->  00:09:17,820
loaded disgust into memory at that instance.

117

00:09:17,840  -->  00:09:20,750
Does this initialiser block will be run OK.

118

00:09:20,850  -->  00:09:26,570
And that would happen if if we make an instance if an instance of this BASIX democracy is made or if

119

00:09:26,580  -->  00:09:32,120
one of these which one of these static methods are in will only at that point of time the class will

120

00:09:32,120  -->  00:09:36,060
be loaded into the memory and this static block will be executed.

121

00:09:36,260  -->  00:09:40,710
OK so there are certain rules when a class has access for the first time and that we will see in the

122

00:09:40,700  -->  00:09:42,390
JVM internals chapter.

123

00:09:42,380  -->  00:09:43,930
So let's not worry about that.

124

00:09:44,000  -->  00:09:53,710
Also here let me just go back and just reiterate here.

125

00:09:54,060  -->  00:10:00,730
So this was working but if you do this Lun do equal to.

126

00:10:01,080  -->  00:10:08,070
So if you really initializing this way then once again it's going to be it's going to give us a compilation

127

00:10:08,070  -->  00:10:13,970
error because it's not the constant variable as it is not initialized in the same line as the declaration

128

00:10:14,120  -->  00:10:14,730
.

129

00:10:14,730  -->  00:10:16,310
OK so that's about it.

130

00:10:16,320  -->  00:10:19,510
So that's about constant variables and that's it.

131

00:10:19,590  -->  00:10:20,180
Thank you.

132

00:10:20,230  -->  00:10:21,020
I'm happy coding
