What is the difference between the ending, Finally and finish in Java?

Asked

Viewed 180 times

-1

What is the difference between the terms: final, finally and finalize(), in the programming language Java?

  • 4

    In addition: https://answall.com/q/27529/101, https://answall.com/q/38303/101 and https://answall.com/q/172909/101 I even thought about closing for other reasons, after all comparisons between things that are puppies, hot dog and dog subject don’t make much sense except for linguistics, the only thing these three things have in common is that they start with final.

1 answer

4


final:

final is used to apply constraints on class, method and variable. A final class cannot be inherited, the final method cannot be replaced and the end value of the variable cannot be changed.

Example:

class FinalExample{  
    public static void main(String[] args){  
        final int x=100;  
        x=200; //Compile Time Error  
    }
}  

finally:

finally is used to put an important code, it will be whether the exception is dealt with or not.

class FinallyExample{  
    public static void main(String[] args){  
        try{  
            int x=300;  
        } catch(Exception e){
            System.out.println(e);
        } finally{
            System.out.println("finally block is executed");
        }  
    }
}  

finalize:

finalize is used to perform cleaning processing before the object is collected as garbage.

class FinalizeExample{  
    public void finalize(){
        System.out.println("finalize called");
    }  

    public static void main(String[] args){  
        FinalizeExample f1=new FinalizeExample();  
        FinalizeExample f2=new FinalizeExample();  
        f1=null;  
        f2=null;  
        System.gc();  
    }
} 

Reference Material

  • @Carlosheuberger you’re right, thank you so much for your contribution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.