Java Final Keyword Tutorial - Learn Final Variables, Methods and Classes

Sharing buttons:

hey what's up everybody today we're

looking at the final keyword this will

be an interesting one also if you're new

here my name is Alex lead on this

channel I make a java tutorial just like

this one every single week so if you're

new here you might be interested in that

then consider subscribe so we're gonna

clips let's just make a new Java project

we'll call it like final keyword fun

that's fun at the end is we want to have

fun here new class call it final and hit

the main method the final keyword means

final value what it's set to is the

final value that it's going to be it's

not gonna change anywhere you can't

change this variable its final it's done

I can't change you can throw the final

keyword in front of a class a variable

or method so I'm gonna show you an

example of each of those how's that


I've got our class here which is a

blueprint for an object and it has the

main method which is gonna help us run

some code we're gonna start with final

variables then go to final methods and

then final classes usually for final

variables they go up here this is what

the class knows that's gonna stay

constant it's never gonna change

also known as global variables global

variables use the final keyword for

example we can have like a max value

final int max equals 5 we can also have

a min value min equals 0 it can also be

static static final double PI we could

have PI in here if we want in 3.14159

let's print out those examples by

creating a final class final F equals nu

final doing F dots max print that out

we'll see that F dot max is equal to 5

I'll delete the other two to avoid

confusion you can also have a blank

final like this but you have to

initialize it so what you would do here

is if for some reason you're required

so your assignment was to have a final

hit wasn't initialized which usually

doesn't happen but you just throw it in

a constructor and you'd say max equals

five so that way when you create an

object here final is the constructor so

it runs code inside the constructor and

now we're setting max to five so if we

save and run this we get five in the

error goes away because we're

initializing it in the constructor but

usually that doesn't happen usually

you'll just set it all at once up here

but I just wanted to let you know that

you can do that the only difference

between a normal variable and a final

variable is that you can't you can't

change the final variable if we had a

method here called change max and passed

in a new Max value we'll call it a new

mix and then somehow we wanted to do max

equals new max we can't do that because

it's final we can't change it but if

this was a regular variable then we

could it's gonna control Z this the

reason these are capital is because it's

good practice to have final variables

capital so that you don't confuse them

with regular variables so when you try

to reassign a variable and it's an all

caps you're like oh that's actually

final so I can't change it so it's just

better to kind of separate the variable

names by putting finals as capitals so

that's final variables in a nutshell

let's do a final method make a regular

method public void say hi I really like


say hi methods I've been doing with me

just because they're so simple now let's

use it f dots

say hi it actually doesn't need to be in

a print statement anymore hi now let's

change that to final it's been a final

keyword in front and see what happens

save and run

and it does the same thing that's

because we're not technically changing

the method so it works fine but if we

had another class that tried to change

that method we can't because final is

final that's it you can't change it

that's the final form here's an example

public class other class extends final

delete that public so that we can put it

in the same file this could also be

implements because extends and

implements takes the methods from the

class above it let's make it here public

void say hi prints if we print hello

hello we get a red underline because you

cannot override the final method from

final so that's an example of a final

method you just can't change a final

method or a final anything so now let's

go on to a final class by the way this

is stuff that would have confused me so

much in my courses I swear I didn't know

what the final keyword really was until

like a few months ago and I graduated a

year and a half ago that's how bad the

computer science education industry is

they just gotta teach it simply the

first time it's a rant let's make a

final class now remember final means

it's the final form it can't change so

if we had final public class final and

we wanted to make another class of the

class and have it extend final like

before we actually can't do that because

now since we use extends extends means

inherits which means other class is now

a subclass of final and since this one's

final it's technically making a new

version of it so we can't do that and

they remove the error message here says

type other class cannot subclass the

final class so the moral of the story is

you can't change anything with the final

keyword in front of it can't change

variables can't change methods you can't


subclasses and that's really it I don't

want to go into more details because

this is really this is really all you

need to know you can also have final

variables inside of other methods so

final int I equals 0 but then if you

tried to set I equal to something else

it works exactly the same you can't

change a final variable and yeah that's

that's basically it I hope this was


have a great rest of your day and I'll

see you in the next video