The meaning of final in java is: -applied to a variable means that the respective variable once initialized can no longer be modified
private final double numer = 12;
If you try to modify this value, you will get an error.
-applied to a method means that the respective method can't be override
public final void displayMsg()
{
System.out.println("I'm in Base class - displayMsg()");
}
But final method can be inherited because final keyword restricts the redefinition of the method.
-applied to a class means that the respective class can't be extended.
class Base
{
public void displayMsg()
{
System.out.println("I'm in Base class - displayMsg()");
}
}
The meaning of finally is :
class TestFinallyBlock{
public static void main(String args[]){
try{
int data=25/5;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
in this exemple even if the try-catch is executed or not, what is inside of finally will always be executed. The meaning of finalize:
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();
}}
before calling the Garbage Collector.