What is final, finally and finalize in Java?

In this Article we will get know the significance of final, finally and finalize in Java.

final:

Final is a keyword in Java,which can be applied to variables, methods, class and local variables in Java

What is final variable in Java?

The variable which is declared with ‘final’ is know as final variable, It can be either member variable or local variable.
Final variable should be initialized only once and cannot be changed.

Example :

 What is final method in Java ?

The method which is declared with ‘final’ is know as final method.
final method can not be overridden in sub class,we cant able to change the functionality of a method when we made a metod as ‘final’.
Final methods are faster than non-final methods.
Example :

 What is final class in Java ?

The Java class with final modifier is called as “final class”.
Final class is can be implemented only once and can not be sub classed or inherited.
for example there are few of the classes in Java are final e.g. String, Integer and other wrapper classes.

 

finally:

finally is a block which used to keep set of executable code statements .
Always finally block is executed even if an unexpected exception occurs in java program.
Basically finally block is used to cleanup the resources those are opened in earlier in the program .

 

finalize:

finalize() method is a method which is defined in java.lang.Object class.
finalize method is called by garbage collector before collecting object and if not intended to be called like normal method.
There is a way you can running of finalize method by calling System.runFinalization() and
Runtime.getRuntime().runFinalization(). These methods ensures that JVM call finalize() method of
all object which are eligible for garbage collection and whose finalize has not yet called.

 

Posted in InterviewQuestions and tagged , , .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">