final vs finally vs finalize in Java

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 พ.ย. 2024

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

  • @emalr.dawlatzai3083
    @emalr.dawlatzai3083 4 ปีที่แล้ว +2

    Very well explained. You should get the award for using the best teaching methods, specially for someone new to Java.

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

    Hi Naveen I'm from Afghanistan i follow your Java Tutorial , your explain is so good , thanks

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

    Thank you so much Naveen... perfect explanation

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

    Perfect explanation and concept clarification.Loved it.Thanks

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

    Your videos are helping me a lot!! Thanks, Naveen.

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

    Hi ,Thank you very much for you patient teaching methods. Just one question on finally, i followed you example! For me its not printing the error in sequence, but in your method its coming in sequence ============================================================
    public static void Division(){
    int i=10;

    try{
    System.out.println("inside try div");
    int k = i/0;
    }
    catch(NullPointerException e) {
    System.out.println("Inside AE catch");
    }
    finally {
    System.out.println("Execute method inspite of exception");
    }
    }
    ==================================================================
    My console:-
    inside try div
    Execute method inspite of exception
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at JavaBasics.FinallyConcept.Division(FinallyConcept.java:38)
    at JavaBasics.FinallyConcept.main(FinallyConcept.java:7)
    =====================================================================================
    I think it should 1st display exception and then display Finally comment like this:-
    inside try div
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at JavaBasics.FinallyConcept.Division(FinallyConcept.java:38)
    at JavaBasics.FinallyConcept.main(FinallyConcept.java:7)
    Execute method inspite of exception
    =====================================================================================
    Please Advise ,what could be wrong here!?

  • @srinathsrinath24
    @srinathsrinath24 6 ปีที่แล้ว

    Superb Explanation with simple way to understand..

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

    awsomely explained Naveen , thank you soo much :)

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

    Very well explained Naveen

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

    Very very useful. Please post continuously..

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

    Thank you so much for an awesome video.. It's really helpful..

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

    Thank you very much for your knowledge sharing

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

    Gud explanation in short duration. Please prepare some videos on SQL.

  • @subhanbasha1
    @subhanbasha1 4 ปีที่แล้ว

    Hello Naveen Sir.
    it is very understandable way of teaching of every concept.
    Thanks for your efforts for all of us.
    for finalize concept, am getting displayed "Finalize Method" two times in console for ur mentioned code and why?

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

    Interview question -
    1. Some real time example for Finalize...How you have implemented in your framework
    2. What will happen if we use System.exit() in catch block. Does the finally block will execute even though ?

    • @AshishDubeyash4you
      @AshishDubeyash4you 6 ปีที่แล้ว

      Yes it will execute finally block if you use System.exit(0) in try block or in Catch block.
      package finallyy;
      public class FinallyBlockConcept {

      public static void main(String[] args) {

      int a = 9;
      int b = 0;

      try {

      int c = a/b;

      }catch(Exception E)
      {
      System.exit(0);
      }
      finally
      {
      System.out.println("Execute Finally Block");
      }


      }
      }

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

      @@AshishDubeyash4you No bro. finally will not be executed. please try running this code itself. it doesnt run. system.exit will terminate the execution fully.

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

    Naveen, for the first example, even if you do not write inside the finally block it will still execute. I am confused, what is the use of finally in this case?
    package javabasics;
    public class Finally {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
    System.out.println("inside try block");
    throw new RuntimeException();
    }

    catch(Exception e) {
    System.out.println("Exception handled");
    }

    System.out.println("Inside finally block");
    }
    }

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

      yes, it's because you have already handled the exception in catch block if any exception encountered which is not handled in catch block your last print line will not execute without using Finally Block. He just showed for your reference.

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

    Nice video, For the benefit of others, The method finalize is deprecated from Java 8+

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

      Ita deprecated from java 9. Now we have cleaner class, replacement of finalize.

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

    As usual, Nice explaination...

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

    If you wanna call GC forcefully, finalize() is required 😊

  • @satyaranjan2k
    @satyaranjan2k 6 ปีที่แล้ว

    Hi Naveen, finalize() is getting executed multiple times in my eclipse and it's repeating exactly same nos of times as the nos of objects created.
    But in your video, though you have 2 objects for the FinalizeConcept concept class, the finalize() is getting executed only once.
    Below is the code which I have written, Please clarify :
    package TestCases;
    public class FinalizeConcept {

    public void finalize()
    {
    System.out.println("finalize method");
    }
    public static void main(String[] args)
    {

    FinalizeConcept f1 = new FinalizeConcept();
    FinalizeConcept f2 = new FinalizeConcept();

    f1 = null;
    f2 = null;

    System.gc();
    }
    }
    O/P :
    finalize method
    finalize method

    • @manikantaraju2592
      @manikantaraju2592 6 ปีที่แล้ว

      U have created 2objects with 2different reference variable so u are getting output twice.
      Comment one Object and try to execute it u will get the required output

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

      But example shown in vedio is has 2 objects but still its displaying finalize method only once

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

      @@jayalakshmikrishnan1956 Happening same with me too . FinalizeMethod output is coming twice

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

    Very Well Explain sir

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

    Thank you Naveen

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

    i am a bit confused with the finalize.....if its a keyword then its color should be changed ...System.gc will look any finalize method present in the class and execute it?

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

      Got it only when we are making the ref variable as null then gc will come into picture and it will look for exactly "finalize" method declared before main and will execute it

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

    To ellaborate more: The purpose of the finalize() method is to allow an object to perform any necessary cleanup or finalization before it is destroyed. We could use this to release resources such as file handles, network connections, or database connections, as well as performing any other necessary cleanup tasks.
    Source: OpenAI/ChatGPT 🙃

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

    Hi Naveen thanks a lot for video , but when i run this program i'm getting the finally block executed first and then the ArithmeticException divide by zero exception is thrown video timing 18:32

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

    Programs for Prime numbers and Fibonacci series please add

  • @Shreysanthu
    @Shreysanthu 5 ปีที่แล้ว

    How to know and verify that finalize is done cleanup processing of the object before garbage collection

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

    Hi Naveen, good evening, I tried with finalize method but i got twice finalize method as the output(2 times printed on the console). Please help me out.

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

    nice explanation

  • @vhvl3888
    @vhvl3888 5 ปีที่แล้ว

    nice ...
    explain throw and throws also

  • @ramyab6961
    @ramyab6961 6 ปีที่แล้ว

    Thank u so much Naveen.

  • @ujjwalverma7787
    @ujjwalverma7787 6 ปีที่แล้ว

    when I run finalize concept then its give an error
    Note: finalizeconcept2.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    please some help me to resolve this error !

  • @bharatvarshney2666
    @bharatvarshney2666 6 ปีที่แล้ว

    nice explanation between of these

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

    Thanku sir

  • @seshareddy7254
    @seshareddy7254 6 ปีที่แล้ว

    Is Catch compulsory for try block..

    • @AshishDubeyash4you
      @AshishDubeyash4you 6 ปีที่แล้ว

      No, but if any exception occured & is not caught then JVM will throw error at Runtime.

  • @bhargavpinnam733
    @bhargavpinnam733 6 ปีที่แล้ว

    Thank you Naveen!

  • @emonyousufy5670
    @emonyousufy5670 4 ปีที่แล้ว

    Thanks Naveen

  • @lisazhou7776
    @lisazhou7776 4 ปีที่แล้ว

    very good. thanks.

  • @rahulprakash6367
    @rahulprakash6367 6 ปีที่แล้ว

    very nice

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

    garbage collecter guy come😂

  • @sridevireddy1371
    @sridevireddy1371 6 ปีที่แล้ว

    Thank you Naveen