Final Keyword in Java Full Tutorial - Final Classes, Methods, and Variables

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ต.ค. 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...

ความคิดเห็น • 115

  • @adrianlowery7175
    @adrianlowery7175 2 ปีที่แล้ว +65

    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

    • @turuus5215
      @turuus5215 2 ปีที่แล้ว +5

      He is pretty good looking too.

  • @sebon11
    @sebon11 ปีที่แล้ว +6

    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

  • @5n2joelemmanuel54
    @5n2joelemmanuel54 ปีที่แล้ว +3

    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

  • @dname1802
    @dname1802 7 หลายเดือนก่อน

    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 :)

  • @ralu0filth
    @ralu0filth 8 หลายเดือนก่อน +1

    Dude, you have a video for every question I have! Love it! Thank you, it really helps! Keep it up!

  • @snsnsn3
    @snsnsn3 6 หลายเดือนก่อน

    The way you explain the ideas behind the scenes really makes it tangible. Thanks :)

  • @gabrielfono844
    @gabrielfono844 ปีที่แล้ว

    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

  • @preethimarineni
    @preethimarineni ปีที่แล้ว

    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.

  • @francksgenlecroyant
    @francksgenlecroyant 2 ปีที่แล้ว

    I wish TH-cam could make it possible to give a video a like any number of times i want. John is a Hero! Thanks John!

  • @teaadvice4996
    @teaadvice4996 2 ปีที่แล้ว +1

    You're missing the static keyword video. And I need it john!!!!

  • @khiemdo5527
    @khiemdo5527 2 ปีที่แล้ว +4

    Great tutorial for Final keyword in Java. Great appreciated for the video , John. I will pass my Java interview with your videos. Happy holidays

  • @burgerbob7324
    @burgerbob7324 ปีที่แล้ว +1

    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.

  • @Anderzon1994
    @Anderzon1994 2 ปีที่แล้ว +3

    Thanks a lot John. I've been learning many things with your videos.

  • @gema9835
    @gema9835 ปีที่แล้ว +1

    why do i love this guy !!! omg!!

  • @gopalkannan4934
    @gopalkannan4934 11 หลายเดือนก่อน

    Great teacher know how to explain easy way.

  • @Clean_Slate
    @Clean_Slate 2 ปีที่แล้ว +3

    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 🍺

  • @jvsnyc
    @jvsnyc 3 ปีที่แล้ว +11

    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.

  • @sarahmahyoub5241
    @sarahmahyoub5241 ปีที่แล้ว +1

    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!

    • @Rateebhatt
      @Rateebhatt 5 หลายเดือนก่อน

      Hey can u explain why

  • @YerushalayimShelZahv
    @YerushalayimShelZahv 2 ปีที่แล้ว +3

    John, love the videos. Thank you for making them.

  • @matthewbrightman3398
    @matthewbrightman3398 2 ปีที่แล้ว

    I finally get it! Ura great teacher John, don’t ever change!

  • @keyvan4680
    @keyvan4680 ปีที่แล้ว

    Thank you very much for the clear explanation.

  • @arnavtumbde6249
    @arnavtumbde6249 ปีที่แล้ว

    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 !

  • @micleh
    @micleh 2 ปีที่แล้ว +2

    As always, very helpful, comprehensive and well-explained video.

  • @desmondwhite8179
    @desmondwhite8179 2 ปีที่แล้ว

    very concise explanations

  • @Simon6oz
    @Simon6oz 2 ปีที่แล้ว

    thank you for this video. its very help full for get some knowledge

  • @aref111
    @aref111 2 ปีที่แล้ว

    Thanks a lot, you are concise and fluent I even enjoy watching your videos on topics that I already know! please keep that up.

  • @kishoremaroju2720
    @kishoremaroju2720 ปีที่แล้ว

    Your channel is just awesome. Can you please make a video on Java-8 time and time zones, thanks.

  • @einaveldaz2700
    @einaveldaz2700 9 หลายเดือนก่อน

    Thank you for the video. Help me so much ❤

  • @D_Elchapo
    @D_Elchapo 6 หลายเดือนก่อน

    this bold man is impressive

  • @kalyankumar8629
    @kalyankumar8629 2 ปีที่แล้ว

    Great toutorial session! SIR.

  • @utkarshk2
    @utkarshk2 ปีที่แล้ว

    Your courses are great and precise and talk all concepts that need to be known . Best for quick revision. :)

  • @hangyeollee-creuset5108
    @hangyeollee-creuset5108 2 ปีที่แล้ว +3

    Thank you for your awesome video ! I am so glad that I found you on TH-cam.
    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

    • @CodingWithJohn
      @CodingWithJohn  2 ปีที่แล้ว +8

      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.

  • @rajeshg3570
    @rajeshg3570 2 ปีที่แล้ว +2

    Hey John, Can you please make a video on static keyword

  • @e10etcreature43
    @e10etcreature43 2 ปีที่แล้ว

    Great Videos,Best Java Channel 🙏🔥🔥🔥🔥.

  • @adipurnomo5683
    @adipurnomo5683 2 ปีที่แล้ว

    Simple and great explanation

  • @chris07ism
    @chris07ism ปีที่แล้ว

    Amazing as always. A great and concise explanation!

  • @akvloggsofficial
    @akvloggsofficial ปีที่แล้ว

    Great , Really helpful 👏

  • @rajeshg3570
    @rajeshg3570 2 ปีที่แล้ว +4

    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 ?

    • @CodingWithJohn
      @CodingWithJohn  2 ปีที่แล้ว +5

      Exactly right! It's a good way to guarantee that the parameter won't be reassigned throughout the execution of the method.

    • @andreassumerauer5028
      @andreassumerauer5028 2 ปีที่แล้ว

      @@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.

  • @fr1ddl33
    @fr1ddl33 2 ปีที่แล้ว

    You got me at "Double pie sounds really good".

  • @samkelobhongoza7994
    @samkelobhongoza7994 2 ปีที่แล้ว +1

    Can you do Tutorial where you build a chess engine with Java, I've been searching on TH-cam I can't find any and ever since I've been learning from your channel I've been improving

  • @smiley___face
    @smiley___face 2 ปีที่แล้ว

    Finally! Ive been waiting for this video for so long 😂

  • @syarifuddinsakri7008
    @syarifuddinsakri7008 ปีที่แล้ว

    Thank you john. very helpful

  • @hristoplamenov1942
    @hristoplamenov1942 2 ปีที่แล้ว

    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.

    • @CodingWithJohn
      @CodingWithJohn  2 ปีที่แล้ว +4

      We'll eventually get there I'm sure!

  • @gayanjayalathbandara4604
    @gayanjayalathbandara4604 2 ปีที่แล้ว

    well explained... thank you

  • @danielcarpenter5250
    @danielcarpenter5250 ปีที่แล้ว

    thanks. i really like your content.

  • @kaledbrahmi3442
    @kaledbrahmi3442 3 ปีที่แล้ว

    Awesome explanation, thank you

  • @ff-gc3vu
    @ff-gc3vu 2 ปีที่แล้ว

    Thank you, that was super clear and great 😊

  • @alihaydarkaya3336
    @alihaydarkaya3336 2 ปีที่แล้ว

    Well done John. Thank you

  • @collegeStudent7373
    @collegeStudent7373 ปีที่แล้ว

    Thank you sir 😊

  • @showmealldblueprints
    @showmealldblueprints 2 ปีที่แล้ว +1

    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.

    • @adipurnomo5683
      @adipurnomo5683 2 ปีที่แล้ว

      Static in variabel mean it can call without create instance class

  • @homelylad
    @homelylad 11 หลายเดือนก่อน

    Didn't u have to use override when you first implemented the method eat within the dog class ?

  • @farzadarian5993
    @farzadarian5993 ปีที่แล้ว

    I ❤ the way you teach

  • @hennesseylei7398
    @hennesseylei7398 ปีที่แล้ว

    thank you for this

  • @tahaamca2739
    @tahaamca2739 2 ปีที่แล้ว

    I love your channel thank you for the best content in youtube.
    Also you look like Michael from Vsauce lol

  • @mdanisansari6657
    @mdanisansari6657 2 ปีที่แล้ว

    John, can you create a similar video for static keyword..

  • @vedd2603
    @vedd2603 3 ปีที่แล้ว +3

    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

    • @CodingWithJohn
      @CodingWithJohn  3 ปีที่แล้ว +5

      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).

  • @fromnothingtoeverything1419
    @fromnothingtoeverything1419 2 ปีที่แล้ว

    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

  • @khalilbensaid9105
    @khalilbensaid9105 ปีที่แล้ว

    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

  • @USA-Visit
    @USA-Visit 2 ปีที่แล้ว

    you the man!

  • @altamashsabri8142
    @altamashsabri8142 ปีที่แล้ว

    Thanks a lot

  • @MTB_Bay_Area
    @MTB_Bay_Area 2 ปีที่แล้ว

    Thank you

  • @mrboss8790
    @mrboss8790 2 ปีที่แล้ว

    Woah! this helped me!

  • @jakepanda209
    @jakepanda209 2 ปีที่แล้ว

    Please mention what's the name of your IDE which you are writing java program... as well as theme

    • @CodingWithJohn
      @CodingWithJohn  2 ปีที่แล้ว

      This is Eclipse, with a dark theme plugin called Darkest Dark. In newer videos I'm using IntelliJ

  • @iMmD-dy5fn
    @iMmD-dy5fn 4 หลายเดือนก่อน

    Thanks ❤

  • @suryapranesh2081
    @suryapranesh2081 2 ปีที่แล้ว

    What is the difference between static method and final method because it is not possible to override both?

  • @FreshThanatos
    @FreshThanatos 2 ปีที่แล้ว

    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?

    • @CodingWithJohn
      @CodingWithJohn  2 ปีที่แล้ว +1

      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.

    • @FreshThanatos
      @FreshThanatos 2 ปีที่แล้ว

      @@CodingWithJohn Amazing. Thanks John. I love your work. I learn something with each of those Videos so keep it up. And thanks for answering

  • @ananthakrishnanvs4543
    @ananthakrishnanvs4543 ปีที่แล้ว

    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

  • @skabbakk3184
    @skabbakk3184 3 ปีที่แล้ว

    Thank you 😍

  • @Anic21212
    @Anic21212 2 ปีที่แล้ว

    Munch Munch Munch , Nom Nom Nom 😁. Thanks for great video.

  • @jackofnotrades15
    @jackofnotrades15 2 ปีที่แล้ว

    do one on static keyword

  • @irvingalejandro8303
    @irvingalejandro8303 3 ปีที่แล้ว +2

    I love the videos

    • @CodingWithJohn
      @CodingWithJohn  3 ปีที่แล้ว

      Thanks, and thanks for watching!

  • @SkyyySi
    @SkyyySi ปีที่แล้ว +1

    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.

  • @vladthe_cat
    @vladthe_cat ปีที่แล้ว

    public static final String countdown = "It's the final countdown!"

  • @3athomy1
    @3athomy1 2 ปีที่แล้ว

    what about the final as a function parameter ?

  • @yami3251
    @yami3251 2 ปีที่แล้ว

    new subscriber here👋

  • @ribasoul
    @ribasoul ปีที่แล้ว

    I thank you

  • @jackgame8841
    @jackgame8841 ปีที่แล้ว

    i am strruggling with it, so it is just static type checking.

  • @selvaperumal100
    @selvaperumal100 ปีที่แล้ว

    Which ide is this sir

  • @stephanievulcheva8125
    @stephanievulcheva8125 ปีที่แล้ว

    What about final instances?

  • @KeringKirwa
    @KeringKirwa 9 หลายเดือนก่อน

    am still struggling with the real world user case of this final thing !

  • @kaushalendrapandey2662
    @kaushalendrapandey2662 2 ปีที่แล้ว

    Best💖

  • @scyye-gaming
    @scyye-gaming 2 ปีที่แล้ว +5

    R.I.P technoblade

  • @zimbabwe8189
    @zimbabwe8189 3 ปีที่แล้ว

    I like how he looks so happy explaining it kkkkk

  • @Pmc987
    @Pmc987 2 ปีที่แล้ว

    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

  • @ishuuuuuuuuuuuuuu
    @ishuuuuuuuuuuuuuu หลายเดือนก่อน

    🔥🔥

  • @InterviewMindsAtWork
    @InterviewMindsAtWork 4 หลายเดือนก่อน

    Java One Liner Code, Very Basic to know for Java programmers,
    th-cam.com/play/PLUPFEhEXH0fxH8DFJJOL6RW7Og4LewPL8.html

  • @starlinetor5791
    @starlinetor5791 10 หลายเดือนก่อน

    so in practice final is just to prevent yourself from making mistakes laters

  • @alinadeem8947
    @alinadeem8947 2 ปีที่แล้ว

    Someone can still remove the final keyword from Pi and overwrite it isnt it? WHats the point here just trying to understand

    • @mr.dingleberry4882
      @mr.dingleberry4882 ปีที่แล้ว

      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.

  • @rodrigoqteixeira
    @rodrigoqteixeira 2 ปีที่แล้ว

    final in varibles: create constants

  • @dilln2158
    @dilln2158 3 ปีที่แล้ว +1

    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.

    • @CodingWithJohn
      @CodingWithJohn  3 ปีที่แล้ว

      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.

    • @dilln2158
      @dilln2158 3 ปีที่แล้ว

      @@CodingWithJohn Thanks!

  • @tuba9800-m4s
    @tuba9800-m4s 11 หลายเดือนก่อน

    2.07

  • @tito6121
    @tito6121 2 ปีที่แล้ว

    watched

  • @abemelekdaniel8913
    @abemelekdaniel8913 2 ปีที่แล้ว

    for his video 0.5x is necessary

  • @jeanehu-pludain6640
    @jeanehu-pludain6640 2 ปีที่แล้ว

    final class is bad

  • @hedgehog337
    @hedgehog337 3 หลายเดือนก่อน

    rainbow like button.........

    • @CodingWithJohn
      @CodingWithJohn  3 หลายเดือนก่อน

      😐

    • @hedgehog337
      @hedgehog337 3 หลายเดือนก่อน

      @@CodingWithJohn ? I just haven't seen it before so it was impressive

    • @hedgehog337
      @hedgehog337 3 หลายเดือนก่อน

      @@CodingWithJohn i didn't understand why you put "😐" 😕