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