MainMenu

Home Java Overview Maven Tutorials

Tuesday 26 December 2017

Difference between Final, Finally and Finalize in Java



Hello friends,

In this article we will learn what is the difference between Final, Finallyand Finalize with suitable examples.

In java, where we want restriction, we use Final Keyword.
We can use Final keyword with class, Method and variables.
Final Class can't be Inherited.
Final method can't be overridden.
Final variable can't be changed.

Finally is a block and the code written inside the Finally block will be executed whether exception is handled or not.

Finalize is a Method and it is used to perform clean up processing just before object is garbage collected.

Example of Final in java


public class Finalandfinally
{
public static void main(String args[])
{
final int x = 10;
int y = x+10;
x = y; //here you will get the error "Remove final modifier of x"
System.out.println("New value of x is :" + x);
Test2 obj = new Test2();
obj.sum();
}
}
final class Test1
{
final public void sum()
{
System.out.println("Hello i am inside method sum");
}
}
class Test2 extends Test1 //Remove final modifier of Test1
{
public void sum() //"Remove final Modifier of Test1.sum(....)"
{
System.out.println("Hello i am inside method sub");
}
}


Finally block

The finally block always executes when the try block exits.
This ensure that the finally block is executed even if an expected exception occurs.

public class Finalandfinally
{
public static void main(String args[])
{
try
{
System.out.println("Enter your no");
Scanner obj = new Scanner(System.in);
int x = obj.nextInt();
System.out.println("Enter your second number");
int y = obj.nextInt();
int z = x/y;
System.out.println("Your result is :" +z);
}
catch(ArithmeticException e)
{
System.out.println(e.getMessage());
}
finally
{
System.out.println("I will be executed whether exception occur or not");
}
}
}

finalize Method

1. Finalize is called before Garbage collector reclaim the object.
2.Its last chance for any object to perform cleanup activity i.e. releasing any system resources held , closing connection if open etc.
3.finalize() method is defined in java.lang.Object class
4.It's the responsibility of developer to call superclass finalize method when we override finalize method.
5.There is no gurantee that finalize method is called because finalize() method is called just before the garbage collection process happens and we are not sure when garbage collection process happens.
6.Exception propagated by finalize() method are not propogated and are ignore by garbage collector.
7.we can force jvm to call finalize method by calling
System.run.Finalization()
Runtime.getRuntime().runFinalization()
8.finalize() method gets called only once by garbage collector.

import java.util.Scanner;
import org.apache.poi.util.SystemOutLogger;
class A
{
int i = 50;
@Override
protected void finalize() throws Throwable
{
System.out.println("From Finalize Method");
}
}
public class Finalandfinally
{
public static void main(String[] args)
{
//Creating two instances of class A
A a1 = new A();
A a2 = new A();
//Assigning a2 to a1
a1 = a2; //Now both a1 and a2 will be pointing to same object
//An object earlier referred by a1 will become abandoned
//Making finalize() method to execute forcefully
Runtime.getRuntime().runFinalization();
System.out.println("done");
}
}

No comments:

Post a Comment