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

Комментарии • 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.

  • @scyye-gaming
    @scyye-gaming 2 года назад +5

    R.I.P technoblade

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

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

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

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

  • @rajeshg3570
    @rajeshg3570 2 года назад +2

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

  • @InterviewMindsAtWork
    @InterviewMindsAtWork 4 месяца назад

    Java One Liner Code, Very Basic to know for Java programmers,
    ruclips.net/p/PLUPFEhEXH0fxH8DFJJOL6RW7Og4LewPL8

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

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

  • @hangyeollee-creuset5108
    @hangyeollee-creuset5108 2 года назад +3

    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

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

  • @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 🍺

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

  • @Anderzon1994
    @Anderzon1994 2 года назад +3

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

  • @vladthe_cat
    @vladthe_cat Год назад

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

  • @Anic21212
    @Anic21212 2 года назад

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

  • @ralu0filth
    @ralu0filth 7 месяцев назад +1

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

  • @teaadvice4996
    @teaadvice4996 2 года назад +1

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

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

  • @YerushalayimShelZahv
    @YerushalayimShelZahv 2 года назад +3

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

  • @samkelobhongoza7994
    @samkelobhongoza7994 2 года назад +1

    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

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

  • @homelylad
    @homelylad 11 месяцев назад

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

  • @micleh
    @micleh 2 года назад +2

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

  • @starlinetor5791
    @starlinetor5791 9 месяцев назад

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

  • @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!

  • @gema9835
    @gema9835 Год назад +1

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

  • @KeringKirwa
    @KeringKirwa 8 месяцев назад

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

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

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

  • @jackgame8841
    @jackgame8841 Год назад

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

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

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

  • @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 4 месяца назад

      Hey can u explain why

  • @danielcarpenter5250
    @danielcarpenter5250 Год назад

    thanks. i really like your content.

  • @abemelekdaniel8913
    @abemelekdaniel8913 2 года назад

    for his video 0.5x is necessary

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

  • @rodrigoqteixeira
    @rodrigoqteixeira 2 года назад

    final in varibles: create constants

  • @stephanievulcheva8125
    @stephanievulcheva8125 Год назад

    What about final instances?

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

  • @D_Elchapo
    @D_Elchapo 6 месяцев назад

    this bold man is impressive

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

  • @dname1802
    @dname1802 6 месяцев назад

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

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

  • @e10etcreature43
    @e10etcreature43 2 года назад

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

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

  • @kishoremaroju2720
    @kishoremaroju2720 Год назад

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

  • @snsnsn3
    @snsnsn3 6 месяцев назад

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

  • @tahaamca2739
    @tahaamca2739 2 года назад

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

  • @francksgenlecroyant
    @francksgenlecroyant 2 года назад

    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!

  • @fr1ddl33
    @fr1ddl33 2 года назад

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

  • @hennesseylei7398
    @hennesseylei7398 Год назад

    thank you for this

  • @collegeStudent7373
    @collegeStudent7373 Год назад

    Thank you sir 😊

  • @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 !

  • @Simon6oz
    @Simon6oz 2 года назад

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

  • @selvaperumal100
    @selvaperumal100 Год назад

    Which ide is this sir

  • @mdanisansari6657
    @mdanisansari6657 2 года назад

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

  • @farzadarian5993
    @farzadarian5993 Год назад

    I ❤ the way you teach

  • @jeanehu-pludain6640
    @jeanehu-pludain6640 2 года назад

    final class is bad

  • @USA-Visit
    @USA-Visit 2 года назад

    you the man!

  • @suryapranesh2081
    @suryapranesh2081 Год назад

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

  • @keyvan4680
    @keyvan4680 Год назад

    Thank you very much for the clear explanation.

  • @ribasoul
    @ribasoul Год назад

    I thank you

  • @altamashsabri8142
    @altamashsabri8142 Год назад

    Thanks a lot

  • @einaveldaz2700
    @einaveldaz2700 8 месяцев назад

    Thank you for the video. Help me so much ❤

  • @iMmD-dy5fn
    @iMmD-dy5fn 3 месяца назад

    Thanks ❤

  • @smiley___face
    @smiley___face 2 года назад

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

  • @MTB_Bay_Area
    @MTB_Bay_Area Год назад

    Thank you

  • @ishuuuuuuuuuuuuuu
    @ishuuuuuuuuuuuuuu 23 дня назад

    🔥🔥

  • @utkarshk2
    @utkarshk2 Год назад

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

  • @tuba9800-m4s
    @tuba9800-m4s 10 месяцев назад

    2.07

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

  • @irvingalejandro8303
    @irvingalejandro8303 3 года назад +2

    I love the videos

  • @syarifuddinsakri7008
    @syarifuddinsakri7008 Год назад

    Thank you john. very helpful

  • @matthewbrightman3398
    @matthewbrightman3398 2 года назад

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

  • @gopalkannan4934
    @gopalkannan4934 11 месяцев назад

    Great teacher know how to explain easy way.

  • @3athomy1
    @3athomy1 2 года назад

    what about the final as a function parameter ?

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

  • @chris07ism
    @chris07ism Год назад

    Amazing as always. A great and concise explanation!

  • @kalyankumar8629
    @kalyankumar8629 2 года назад

    Great toutorial session! SIR.

  • @gayanjayalathbandara4604
    @gayanjayalathbandara4604 2 года назад

    well explained... thank you

  • @desmondwhite8179
    @desmondwhite8179 2 года назад

    very concise explanations

  • @ff-gc3vu
    @ff-gc3vu 2 года назад

    Thank you, that was super clear and great 😊

  • @kaledbrahmi3442
    @kaledbrahmi3442 3 года назад

    Awesome explanation, thank you

  • @zimbabwe8189
    @zimbabwe8189 3 года назад

    I like how he looks so happy explaining it kkkkk

  • @akvloggsofficial
    @akvloggsofficial Год назад

    Great , Really helpful 👏

  • @tito6121
    @tito6121 Год назад

    watched

  • @jackofnotrades15
    @jackofnotrades15 2 года назад

    do one on static keyword

  • @yami3251
    @yami3251 2 года назад

    new subscriber here👋

  • @mrboss8790
    @mrboss8790 2 года назад

    Woah! this helped me!

  • @adipurnomo5683
    @adipurnomo5683 2 года назад

    Simple and great explanation

  • @alihaydarkaya3336
    @alihaydarkaya3336 2 года назад

    Well done John. Thank you

  • @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 "😐" 😕

  • @kaushalendrapandey2662
    @kaushalendrapandey2662 2 года назад

    Best💖

  • @skabbakk3184
    @skabbakk3184 3 года назад

    Thank you 😍