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