[java] In Java, what purpose do the keywords `final`, `finally` and `finalize` fulfil?

In Java, what purpose do the keywords final, finally and finalize fulfil?

This question is related to java final finally finalize

The answer is


1. Final • 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. • Final variables are initialized at the time of creation except in case of blank final variable which is initialized in Constructor. • Final is a keyword.

2. Finally • Finally is used for exception handling along with try and catch. • It will be executed whether exception is handled or not. • This block is used to close the resources like database connection, I/O resources. • Finally is a block.

3. Finalize • Finalize is called by Garbage collection thread just before collecting eligible objects to perform clean up processing. • This is the last chance for object to perform any clean-up but since it’s not guaranteed that whether finalize () will be called, its bad practice to keep resource till finalize call. • Finalize is a method.


The meaning of final in java is: -applied to a variable means that the respective variable once initialized can no longer be modified

private final double numer = 12;

If you try to modify this value, you will get an error.

-applied to a method means that the respective method can't be override

 public final void displayMsg()
    {
        System.out.println("I'm in Base class - displayMsg()");
    }

But final method can be inherited because final keyword restricts the redefinition of the method.

-applied to a class means that the respective class can't be extended.

class Base
{

    public void displayMsg()
    {
        System.out.println("I'm in Base class - displayMsg()");
    }
}

The meaning of finally is :

class TestFinallyBlock{  
  public static void main(String args[]){  
  try{  
   int data=25/5;  
   System.out.println(data);  
  }  
  catch(NullPointerException e){System.out.println(e);}  
  finally{System.out.println("finally block is always executed");}  
  System.out.println("rest of the code...");  
  }  
} 

in this exemple even if the try-catch is executed or not, what is inside of finally will always be executed. The meaning of finalize:

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

before calling the Garbage Collector.


The final keyword is used to declare constants.

final int FILE_TYPE = 3;

The finally keyword is used in a try catch statement to specify a block of code to execute regardless of thrown exceptions.

try
{
  //stuff
}
catch(Exception e)
{
  //do stuff
}
finally
{
  //this is always run
}

And finally (haha), finalize im not entirely sure is a keyword, but there is a finalize() function in the Object class.


http://allu.wordpress.com/2006/11/08/difference-between-final-finally-and-finalize/

final – constant declaration.

finally – The finally block always executes when the try block exits, except System.exit(0) call. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

finalize() – method helps in garbage collection. A method that 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.

Did you try searching on google, and need clarification for an explanation?


  • "Final" denotes that something cannot be changed. You usually want to use this on static variables that will hold the same value throughout the life of your program.
  • "Finally" is used in conjunction with a try/catch block. Anything inside of the "finally" clause will be executed regardless of if the code in the 'try' block throws an exception or not.
  • "Finalize" is called by the JVM before an object is about to be garbage collected.

final: final is a keyword. The variable decleared as final should be initialized only once and cannot be changed. Java classes declared as final cannot be extended. Methods declared as final cannot be overridden.

finally: finally is a block. The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling - it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

finalize: finalize is a method. Before an object is garbage collected, the runtime system calls its finalize() method. You can write system resources release code in finalize() method before getting garbage collected.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to final

Difference between final and effectively final How does the "final" keyword in Java work? (I can still modify an object.) when exactly are we supposed to use "public static final String"? What is the purpose of the "final" keyword in C++11 for functions? In Java, what purpose do the keywords `final`, `finally` and `finalize` fulfil? Static Final Variable in Java What is the point of "final class" in Java? Change private static final field using Java reflection Why is there no Constant feature in Java? final keyword in method parameters

Examples related to finally

Try-catch-finally-return clarification In Java, what purpose do the keywords `final`, `finally` and `finalize` fulfil? Java Try Catch Finally blocks without Catch Does C++ support 'finally' blocks? (And what's this 'RAII' I keep hearing about?)

Examples related to finalize

how to destroy an object in java? In Java, what purpose do the keywords `final`, `finally` and `finalize` fulfil? When is the finalize() method called in Java?