Final, Finally and Finalize in java
In the previous post ,we have seen the difference between throw and throws keywords. In this post, we will see the difference between final, finally and finalize keywords.
Let’s start with final keyword.
Final keyword in java:
Final keyword is used to make the class, method and variable restricted which can not be modified once declared.
- If apply on class, class can not be inherited.
- If apply on method, method can not be overridden.
- If apply on a variable, variable’s value can not be changed.
class FinalEx extends TestIt{ public static void main(String[] args){ final int a=10; a=20;//Compile Time Error } void getData(){ //Compile Time Error System.out.println("This is final method"); } } class TestIt{ final void getData(){ System.out.println("This is final method"); } }
Finally block in java:
A final block can be associated with a try block. There are few points which should know about finally block.
1. Finally block will be associated with a try block. Even if try does not have a catch block, finally can be associated with this.
public class FinallyEx { public static void main(String[] args) { getData(); } public static void getData(){ try{ System.out.println("try block"); }finally{ System.out.println("finally"); } } }
2. The code within finally block will be always be executed , even if exceptions occur or not.
3. It is not mandatory to include finally block. But if we do, it will be executed regardless the exception handled in try-catch block.
4. In normal execution, finally block will be executed after try block. But if exception occur, first catch block will be executed and then finally block will be executed.
5. It makes sense to have the clean up code in the finally block, like closing a file or data connection. In any situation, if exception occurs during file handling or database operations , the code inside finally block should close the file and database connection.
6. If we have transfer statements like return, break or continue in try-catch blocks, even though finally block will be executed.
Is there any possibility that finally block will not be executed?
Yes, in some circumstances like as give below.
- Exception occurred in finally block.
- System.exit() method used to forcefully exit.
- Any fatal error causes the application to abort.
public class FinallyEx { public static void main(String[] args) { getData(); } public static void getData(){ try{ System.out.println("try block"); int i = 2/0; }catch(Exception e){ System.out.println("Error caught"); }finally{ System.out.println("finally block executed"); } } }
In the above example, when exception occurs catch block is executed along with finally block.
Finalize method in java:
There are few points to know about finalize.
- finalize() is a method which is defined in java.lang.Object class, that means it is available to all the classes.
- finalize() method is called for clean up process just before the objects are garbage collected.In other words, we can say it is Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
- finalize() is called only once by Garbage collector, if objects can revive themselves then finalize will not be called again.
- This method does not return any value.
public class FinalizeEx { public static void main(String[] args){ FinalizeEx obj=new FinalizeEx(); obj=null; System.gc(); //finalize will be called } public void finalize(){ System.out.println("finalize called"); } }
1 Response
[…] the previous post, we have seen difference between final, finally and finalize. In this post, we will see what is exception […]