Final, finally and finalize in Java

final keyword

final(lowercase) is a reserved keyword in java. We can’t use it as an identifier as it is reserved. We can use this keyword with variables, methods and also with classes. The final keyword in java has different meaning depending upon it is applied to variable, class or method.

1.Final with Variables : The value of variable cannot be changed once initialized.
class A { public static void main(String[] args) { // Non final variable int a = 5; // final variable final int b = 6; // modifying the non final variable : Allowed a++; // modifying the final variable : // Immediately gives Compile Time error. b++; } }

If we declare any variable as final, we can’t modify its contents since it is final, and if we modify it then we get Compile Time Error.

2.final with Class : The class cannot be subclassed. Whenever we declare any class as final, it means that we can’t extend that class or that class can’t be extended or we can’t make subclass of that class.

final class RR { public static void main(String[] args) { int a = 10; } } // here gets Compile time error that // we can't extend RR as it is final. class KK extends RR { // more code here with main method }

3.final with Method : The method cannot be overridden by a subclass. Whenever we declare any method as final, then it means that we can’t override that method.

class QQ { final void rr() {} public static void main(String[] args) { } } class MM extends QQ { // Here we get compile time error // since can't extend rr since it is final. void rr() {} }

Note : If a class is declared as final then by default all of the methods present in that class are automatically final but variables are not.

// Java program to illustrate final keyword final class G { // by default it is final. void h() {} // by default it is not final. static int j = 30; public static void main(String[] args) { // See modified contents of variable j. j = 36; System.out.println(j); } }


Output:
36
Java final example
class Test { public static void main(String[] args) { final int x=200; x=300;//Compile Time Error } }


Output:
Test.java:6: cannot assign a value to final variable x x=300;//Compile Time Error ^ 1 error You shouldn't divide number by zero
Java finally example
class Test { public static void main(String[] args) { try { int x=500; }catch(Exception e){System.out.println(e);} finally{System.out.println("finally block is executed");} } }


Output:
finally block is executed
Java finalize example
class Test { public void finalize(){System.out.println("finalize called");} public static void main(String[] args){ Test f1=new Test(); Test f2=new Test(); f1=null; f2=null; System.gc(); } }


Output:
finalize called finalize called
No.finalfinallyfinalize
1)Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final method can't be overridden and final variable value can't be changed.Finally is used to place important code, it will be executed whether exception is handled or not.Finalize is used to perform clean up processing just before object is garbage collected.
2)Final is a keyword.Finally is a block.Finalize is a method.
Important points:
  • There is no guarantee about the time when finalize is called. It may be called any time after the object is not being referred anywhere (cab be garbage collected).
  • JVM does not ignore all exceptions while executing finalize method, but it ignores only Unchecked exceptions. If the corresponding catch block is there then JVM won’t ignore and corresponding catch block will be executed.
  • System.gc() is just a request to JVM to execute the Garbage Collector. It’s up-to JVM to call Garbage Collector or not.Usually JVM calls Garbage Collector when there is not enough space available in the Heap area or when the memory is low.



Instagram