Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Sunday, 16 July 2017

Difference Between Final, Finally, Finalize In Java

Java Final vs Finally vs Finalize

final, finally, finalize in java

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.


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.


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();
}
}

Share:

2 comments:

  1. Nice post about java finally, finalize, and final

    ReplyDelete
  2. Nice post, Thanks for your sharing

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate