8.13 Final Keyword in Java

Sharing buttons:

in this video we'll say the importance

of final keyword now so when you work

with final keyword let's start with the

first part basically when I talk about

final keyword we have a Cuba's final now

it can be used with a variable it can be

used with a method and we can use final

with the with the with a class so let's

see how it was the use of final variable

so let me make a publisher avoid main

function and let me define a variable

int a let's say it's st. I and the value

for I is 5 okay

and now if I say s out and if I print

the value of I the output you'll be

getting is obviously 5 right because we

are assigning a value and then that's it

we are signing after assignment I have

to I'm just renting it but let's say I

am doing a change here if I say I plus

plus so in this statement the value of I

will become sex and the output will be 6

right and 6 but what if I want to stop

someone by changing these values I would

not want to change the value of I so

what I can do is I can make this

variable as final so if you make a

variable final it means once it is

assigned it will not change even you can

see that we are getting a error here

which it says cannot assign our value to

our final variable simple but what if I

fail to assign the value here and then I

am assigning a value here instead of

doing increment if I say I equal to 6

again it will not laugh

because you cannot change the value

after assignment but let's say if I fail

to assign a value here if I fail to

assign a value at declaration I can

assign it later but once assigned we can

see we are not getting error here

if I run this the values the value will

be 6 but let's say once a sign I'm

changing the value next time if I say I

equal to 7 then it will give me the

error that means your final final

keyword with variable says you're

creating a constant or you're creating a

variable which the value which of will


so that's the importance of final

keyboard with variable let's say what is

a user final keyword with the help with

the method so let me remove this example

here and let me do find a class that say

Class A and I have a Class B I know it

Class A and Class B not a proper name

but for this example you will do so

let's say we have two methods one is

public void show or we have one method

only with this public void show and we

paint in show in a show okay and here

what I'm doing is I'm just saying Class

B exchange Class A which means if I

create object of be here be obj equal to

nu B and if I say obj dot show again in

D we don't have show method right again

with the help of inheritance it will

fetch this show down and then we can

call show if I run this it will say in a

show but what if I say we have a method

here which is public void show and in

this let me print in B show knife around

the code because of the because of the

because of polymorphism the output is in

B show it's because we are doing method

overriding right because this method

show as however I did this method show

but what if I want to stop someone I

don't want anyone to override this

method so what I can do is I can make

this method as final so you can see we

are getting an error here it says you

cannot override our method which is

final clear so that's the advantage of

using a or variable a final keyword a

final keyword with method but let's say

let's go with the last thing which is a

final keyword with a class let's say I

don't have a show method here and here

I'm saying I don't want anyone to

override or to in height a class which

is a I can make this class as

so if you make a class's final no other

class can extend that final class clear

and as the advantage of using the

advantage of using this final keyword

with class okay so this is final with

variable final with method and final

with class