Java Final vs Finally vs Finalize
Now here we will learn what is the difference between final, finally and finalize in java. This is very-very important question for any java interviews.
What is the difference between final and finally and finalize in java is mostly asked question in any java interviews. So let's starts
First we starts with final.
What is the difference between final and finally and finalize in java is mostly asked question in any java interviews. So let's starts
First we starts with final.
Final
- Final is a keyword or non-access modifier in java.
- Final keyword basically used to stop or restricted the users.
- Final keyword can be apply with class, variables, and methods.
- If you make any class as final class then you cannot inherit(extends) this class. If you make any variable as final variable then you cannot change the value of this variable after initialization. If you make any method as final method then you cannot override this method.
- If we declare final variable along with static keyword will become a constants.
Syntax of final keyword
// Final keyword with class
final class Test
{
-----
-----
-----
}
// Final keyword with variable
final int a = 40;
// Final keyword with method
final void run()
{
-----
-----
-----
}
Java Final Keyword Example
In this example we will apply final keyword only with class, if you apply final keyword with class then you cannot inherit that class in java.
final class Test
{
void show()
{
System.out.println("hello");
}
}
class Simple extends Test
{
public static void main(String args[])
{
Simple s = new Simple();
s.show();
}
}
output : compile time error
Finally
- Finally is a block in java.
- Finally block basically used with try and catch block, you can use finally block only with try block or with try or catch.
- Finally block used to execute important code or cleanup processing.
- Finally block is always executes weather the exception is handled or not.
- Finally block must be the last block in the program i.e first declare try block then finally block or first declare try then catch the finally.
Note : There are some situations where finally block doesn't executes e.g if exception occurs in finally block and if you call System.exit() in program.
Java Finally Block Example
class Test
{
public static void main(String args[])
{
try
{
int i = 42/0;//ArithmeticException occurs
System.out.println(i);
}
catch(Exception e)
{
System.out.println("exception is handled");
}
finally
{
System.out.println("finally block always executes");
}
}
}
output : exception is handled
finally block always executes
In the above example, Exception is occurred in try block and catch in catch block and finally block always executes weather exception is handled or not.
In the above example, Exception is occurred in try block and catch in catch block and finally block always executes weather exception is handled or not.
Finalize
- Finalize is a method in java.
- Java finalize() method comes from Object class which is super class in java.
- In java before an object is garbage collected, the garbage collectors calls finalize() method, In other words before removing or destroying unused object or un-referenced object, garbage collector calls finalize() method.
- Java finalize() method generally used to close the connections, stream etc.
Java Finalize Example
class Simple
{
public void finalize()
{
System.out.println("finalize example");
}
public static void main(String args[])
{
Simple s = new Simple();
s = null;
System.gc();
}
}
output : finalize example
Visit for further reading:
OOPS Interview Questions in Java.
Interview Questions on Final Keyword in Java.
Servlet Interview Questions.
JSP Interview Questions.
Difference Between Constructor and Method in Java.
Difference Between String and StringBuffer in Java.
Difference Between Abstract Class and Interface in Java.
Difference Between HashSet and HashMap in Collection Framework.
Visit for further reading:
OOPS Interview Questions in Java.
Interview Questions on Final Keyword in Java.
Servlet Interview Questions.
JSP Interview Questions.
Difference Between Constructor and Method in Java.
Difference Between String and StringBuffer in Java.
Difference Between Abstract Class and Interface in Java.
Difference Between HashSet and HashMap in Collection Framework.
Nice post about java finally, finalize, and final
ReplyDeleteNice post, Thanks for your sharing
ReplyDelete