group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
255 Members
Join this group to post and comment.
DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
10 Feb 2019

What is the difference between final, finally and finalize in java?

This is the most famous question of any interviewer to ask about difference between final, finally and finalize method in java.

Final keyword :

final is a keyword which is used to make a variable or a method or a class as “unchangeable“. In simple terms,

A variable which is declared as final, it’s value can not be changed once it is initialized. A method declared as final can not be overridden or modified in the sub class. A class declared as final can not be extended.

Finally Block :

finally is a block which is used for exception handling along with try and catch blocks. finally block is always executed whether exception is raised or not and raised exception is handled or not. Most of time, this block is used to close the resources like database connection, I/O resources etc.

Finally Vs Finalize() :

But, there is one similarity between finally block and finalize() method. Both are used to close the resources used by the program. finally block is used to close the resources soon after their use. finalize() method is used to close the resources before an object is removed from the memory. That means if you use finalize() method to close the resources, they will remain open until an object,  which is using them, is garbage collected.

But, using finalize() method to close the resources is less recommended as it is not guaranteed that garbage collector will always call finalize() method on an object before it is removed from the memory. If it is not called, the resources will remain open. Therefore, it is always good to close the resources soon after their use using finally block.

AdhikariHarika Reshma
AdhikariHarika Reshma
Computer Science
1mo ago

Difference between final,finally,finalize:

Final-final is a  keyword  that can be used with a class,variable or method.l

1.A variable declared as final acts as constant,which means one a variable is declared and assigned,the value cannot be changed.An object can also be final,which means that once the object is created it cannot be assigned a different object although the properties of fields of the object can be changed.

2.A final class is immutable,which means that no other class can extend from it.E.g,string,integer.

3.A final method in a class cannot be overridden in the child class.

Example:

class Fin
{
  public static void main(String args[])
  {
     final int x=10;
      x=30;//compile time error
  }
}

Finally-finally is a keyword used with try-catch block for handling exception.The finally block is optional in try-catch block.The finally code block is always executed after try or catch block is completed.The general use case for finally block is to close the resources or clean up objects used in try block.For e.g.Closing a FileStream,I/O stream objects,Database connections,HTTP connections are generally closed in a finally block.

Example:

class Example
{
 public static void main(String args[])
 {
  try{
       int x=30;
     }
  catch(Exception e)
    {
     System.out.println(e);
    } 
  finally{
    System.out.println("finally block is executed");
         } 
  }
}

Finalize-This is the method of Object class.It is invoked before an object is discarded by the garbage collector,allowing it to clean up its state.Should not be used to release non memory resources like file handles,sockets,database connections etc because java has only a finite number of these resources and you do not  know when the garbage collection is going to kick in to release these non memory resources through the finalize() method.  

Example:

class Fit
{
  public void finalize()
  {
    System.out.println("finalize called");
  }
  public static void main(String args[])
  {
    Fit f=new Fit();
    Fit f1=new Fit();
    f=null;
    f1=null;
    System.gc();
  }
  
}

Shruthi Pillai
Shruthi Pillai
Information Technology
1mo ago

Final is a keyword .Final is used to apply restrictions on class, method and variable.  Final class can't be inherited, final method can't be overridden and final variable value can't be changed. 

Finally is a block. Finally is used to place important code, it will be executed whether exception is handled or not. 

Finalize is a method. Finalize is used to perform clean up processing just before object is garbage collected. 

Archana Priyadarshini
Archana Priyadarshini
Computer Science
1mo ago

Final is a modifier which you can apply to variable, methods, and classes. If you create a variable final, this means its value cannot be changed once initialised.

Finalise is a method, which is called just before an object is a garbage collected, allowing it a final chance to save itself, but the call to finalise is not definite.

Finally is a keyword which is used in exception handling, along with try and catch. The finally block is always implemented regardless of whether an exception is thrown from try block or not.