Final Keyword in Java Full Tutorial - Final Classes, Methods, and Variables
HTML-код
- Опубликовано: 30 сен 2024
- Complete Java beginner's course: codingwithjohn...
What does "final" do in Java? Learn what the "final" keyword means in 3 different places in your Java programs. Java has final variables, final classes, and final methods, and "final" works differently in each of those places.
So "final" in Java can be a bit confusing, but doesn't have to be! This video gives a complete understanding of "final" and how you can use it in your own Java programs.
Learn exactly what final variables, final classes, and final methods mean in this beginner's Java lesson video.
Learn or improve your Java by watching it being coded live!
Hey, I'm John! I'm a Lead Java Software Engineer and I've been in the programming industry for more than a decade. I love sharing what I've learned over the years in a way that's understandable for all levels of Java developers.
Let me know what else you'd like to see!
Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.
📕 THE best book to learn Java, Effective Java by Joshua Bloch
amzn.to/36AfdUu
📕 One of my favorite programming books, Clean Code by Robert Martin
amzn.to/3GTPVhf
🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
www.audibletria...
🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
bit.ly/3QPNGko
📹Phone I use for recording:
amzn.to/3HepYJu
🎙️Microphone I use (classy, I know):
amzn.to/3AYGdbz
Donate with PayPal (Thank you so much!)
www.paypal.com...
☕Complete Java course:
codingwithjohn...
codingwithjohn...
You are a master teacher. You know in what order to teach concepts, which examples will best convey the concept, and how to simply TEACH! Your personality, your passion, and the feeling of ease that you give off is so amazing. You'd be a great streamer. You're a fantastic communicator
He is pretty good looking too.
R.I.P technoblade
Great video. Sometimes it is reasonable to have a final instance variable, which can be initialized in its definition or in the constructor or an init block, but not later. Not as common as the examples you gave, tho.
Dude, it's impressive how extremely clear you are in how you teach. Seriously, i don't know if there's any second in this tutorial (and all others) that could've been done better. Impressive
You actually SHOULD mark your classes final by default. If your class is final, you are free to unlock inheritance later on, since, before that, nothing could inherit from your class, thus all the implementation details are completely secret to consumers off it. If your class is already non-final, making it final is a backwards incompatible change.
Great tutorial for Final keyword in Java. Great appreciated for the video , John. I will pass my Java interview with your videos. Happy holidays
Hey John, Can you please make a video on static keyword
Java One Liner Code, Very Basic to know for Java programmers,
ruclips.net/p/PLUPFEhEXH0fxH8DFJJOL6RW7Og4LewPL8
man you are amazing. i watched about 10 of your videos and understood each one of them. you are an amazing teacher john. have the utmost respect for you
Could you explain constructors and this operator, private variables inside class how does a programmer understand those it's like they're in different world from me
Constructors cannot be made final in Java, it is not allowed (wouldn't make much sense if it was allowed).
I'm not sure what exactly you're asking with your other question, but I'll attempt to answer it anyway;
Private variables are only visible to the class they are declared in, so you can't read or modify them from any other class. They are declared outside of any method (like PI variable we declared in this video).
They can be static (not tied to any particular instance/object of the class) or non-static (each instance of the class has its own version of that variable).
Thank you for your awesome video ! I am so glad that I found you on RUclips.
I noticed that my IDE asks me to create constructors if I have final variables in the class.
Ex) public class Example {
private final String examples;
Then I am obligated to create a constructor.
But when I have just private variables in the class. IDE doesn't bother me if I create a constructor of this class or not.
Is this because final variables meant to be only assigned once, this is why IDE obligates me to create a constructor of it's class ?
Thank you for taking time to read my comment and have a nice day
Interesting question - in this case you aren't necessarily obligated to create a constructor (although much of the time this is probably what you want to do), but you are obligated to assign a value to this variable in a way that the compiler knows can only happen once, since it's final.
That means you can initialize it inside of a constructor, like you're saying, with something like this:
public Example (String examples) {
this.examples= examples;
}
Or you can just initialize it right where you declare it:
private final String examples = "this is the value";
These are really the only places that the compiler is able to guarantee that it's only going to be set once, so I think that's why you'll see this error.
For example, if you were allowed to set this value inside some other method in your class, like:
private void doStuff(){
this.example = "setting this value";
}
Nothing would prevent this method from being called twice, breaking the rule that a final variable could only be set once. Even if you put it in the main() method, technically that method can be called from anywhere else in your code or even recursively (although it's really weird to do that and generally you probably shouldn't), so you still have the same potential problem.
Excellent video! Thanks for making concepts like that easy to understand while keeping the video short.
I really hope your channel grows big, since I believe it deserves a lot more subs! Cheers 🍺
Awesome explanation.. I've a question here..in some places I've seen final parameters in a method signature. Any specific advantage for that? My guess is , if you have a final parameter in your method signature, then it won't let you assign a value inside the method body. Is that correct ?
Exactly right! It's a good way to guarantee that the parameter won't be reassigned throughout the execution of the method.
@@CodingWithJohn That is correct but the practise also does not make much sense IMO.
While you can assign a new value to a non final parameter inside the function this will never change the variable value on the caller side outside the scope of that function.
Given that the only justification for using final on a parameter is to make sure that a rookie programmer does not think he can return a value change in one of the parameters by reusing that parameter.
Changing parameter values is considered bad coding style by some. To me it isn't an annoyance because I am well aware that reusing a parameter has no side effects outside the function.
Thanks a lot John. I've been learning many things with your videos.
public static final String countdown = "It's the final countdown!"
Munch Munch Munch , Nom Nom Nom 😁. Thanks for great video.
Dude, you have a video for every question I have! Love it! Thank you, it really helps! Keep it up!
You're missing the static keyword video. And I need it john!!!!
I have a doubt
While writing a class i am intializing a private final double pi = 3.14;
and when each time i am creating an instance for that class
it creates memory for that variable each time
why should that be done, because it is a constant
keep in mind it is not declared as static
John, love the videos. Thank you for making them.
Can you do Tutorial where you build a chess engine with Java, I've been searching on RUclips I can't find any and ever since I've been learning from your channel I've been improving
Hello , i want to ask you ! what is the name of theme used with your ide ! i like the combination of colors for class name methods variable etc ... thanks
Didn't u have to use override when you first implemented the method eat within the dog class ?
As always, very helpful, comprehensive and well-explained video.
so in practice final is just to prevent yourself from making mistakes laters
I just found your LinkedIn, 3.95 GPA... This man fucks! What's your advice for CS students trying to achieve a high GPA? Also how important would say GPA is when applying for your first full-time position? I'm going into my sophomore year with a 3.8 GPA.
Yeesh, guess I might be easier to find than I thought.....
A good GPA is nice to have and might break a tie with another job candidate but it's not the be-all-end-all. If it's real bad you'll probably have a tough time finding a first gig, but after that no one will ask.
As for getting a high GPA - don't take Russian Literature. Start your programming assignments early. Computer lab time with other students and tutors can help at lot and you'll absorb a ton watching how other people do things, and is usually just more fun than staying in your room anyway.
@@CodingWithJohn Thanks!
why do i love this guy !!! omg!!
am still struggling with the real world user case of this final thing !
Your channel is awesome, every video i've seen so far is succinct. This is the perfect way to refresh on Java concepts without having to waste time digging through my old courses or forums.
Hey John. In the case you use final in the PI variable, why would you use the static keyword too? If you can't reference anywhere else.
Static in variabel mean it can call without create instance class
i am strruggling with it, so it is just static type checking.
Please mention what's the name of your IDE which you are writing java program... as well as theme
This is Eclipse, with a dark theme plugin called Darkest Dark. In newer videos I'm using IntelliJ
Someone can still remove the final keyword from Pi and overwrite it isnt it? WHats the point here just trying to understand
Its to prevent accidental reassignment. If someone goes out of their way to edit out the final keyword, then that was very much not accidental.
I always wondered why we need to use static with final when it comes to methods. now i know! Amazing job! Subscribed already and i do that rarely!
Hey can u explain why
thanks. i really like your content.
for his video 0.5x is necessary
Just as question. The getter and setter methods could be allways final because I would like them to allways do the same right? But that would not be good practise? Or why arent they allways final when automaticly generated or why arent they allways final if people make examples? Just to safe space?
I could imagine a scenario where a subclass might want to implement a version of a getter or setter with other effects, so they don't have to be final by default. But if you don't want subclasses to be able to do that, final would lock that down.
@@CodingWithJohn Amazing. Thanks John. I love your work. I learn something with each of those Videos so keep it up. And thanks for answering
final in varibles: create constants
What about final instances?
It’s sad that I have over 10 years experience and understand all of your videos, from experience, but technical interviews always wash me out because I can’t remember terms during interviews
this bold man is impressive
I dont know
for some reasons, anytime I have issues , I just jump on your youtube channel and found a perfect solution.
sir , I love what you are doing.
love from herndon virginia
The great thing about these videos, it's not just the 'how to', but also give a real example from your experience. It helps so much to make sense. Thank you so much :)
Have you done how to access private variable?
In c++ we use a function to form like a bridge so to speak
How do you do this in java
Great Videos,Best Java Channel 🙏🔥🔥🔥🔥.
I really admire the way you define , explain tings . I feel it will be more good to ask questions at the end of explaining which helps to think more.
Your channel is just awesome. Can you please make a video on Java-8 time and time zones, thanks.
The way you explain the ideas behind the scenes really makes it tangible. Thanks :)
I love your channel thank you for the best content in youtube.
Also you look like Michael from Vsauce lol
I wish RUclips could make it possible to give a video a like any number of times i want. John is a Hero! Thanks John!
You got me at "Double pie sounds really good".
thank you for this
Thank you sir 😊
Beautiful and impressive content ! So much to the point and really well explained. The most productive channel i found related to coding ! Keep it up !
thank you for this video. its very help full for get some knowledge
Which ide is this sir
John, can you create a similar video for static keyword..
I ❤ the way you teach
final class is bad
you the man!
What is the difference between static method and final method because it is not possible to override both?
Thank you very much for the clear explanation.
I thank you
Thanks a lot
Thank you for the video. Help me so much ❤
Thanks ❤
Finally! Ive been waiting for this video for so long 😂
Thank you
🔥🔥
Your courses are great and precise and talk all concepts that need to be known . Best for quick revision. :)
2.07
Thanks a lot, you are concise and fluent I even enjoy watching your videos on topics that I already know! please keep that up.
I love the videos
Thanks, and thanks for watching!
Thank you john. very helpful
I finally get it! Ura great teacher John, don’t ever change!
Great teacher know how to explain easy way.
what about the final as a function parameter ?
John, keep going with the good explained Java videos! One hint from my side - after explaining the basics, go some levels deeper like Streams, RxJava etc.
We'll eventually get there I'm sure!
Amazing as always. A great and concise explanation!
Great toutorial session! SIR.
well explained... thank you
very concise explanations
Thank you, that was super clear and great 😊
Awesome explanation, thank you
I like how he looks so happy explaining it kkkkk
Great , Really helpful 👏
watched
Responded
do one on static keyword
new subscriber here👋
Woah! this helped me!
Simple and great explanation
Well done John. Thank you
rainbow like button.........
😐
@@CodingWithJohn ? I just haven't seen it before so it was impressive
@@CodingWithJohn i didn't understand why you put "😐" 😕
Best💖
Thank you 😍