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

104,371

Solution 1

final

final can be used to mark a variable "unchangeable"

private final String name = "foo";  //the reference name can never change

final can also make a method not "overrideable"

public final String toString() {  return "NULL"; }

final can also make a class not "inheritable". i.e. the class can not be subclassed.

public final class finalClass {...}
public class classNotAllowed extends finalClass {...} // Not allowed

finally

finally is used in a try/catch statement to execute code "always"

lock.lock();
try {
  //do stuff
} catch (SomeException se) {
  //handle se
} finally {
  lock.unlock(); //always executed, even if Exception or Error or se
}

Java 7 has a new try with resources statement that you can use to automatically close resources that explicitly or implicitly implement java.io.Closeable or java.lang.AutoCloseable

finalize

finalize is called when an object is garbage collected. You rarely need to override it. An example:

protected void finalize() {
  //free resources (e.g. unallocate memory)
  super.finalize();
}

Solution 2

  • "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.

Solution 3

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.

Solution 4

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.

Solution 5

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?

Share:
104,371

Related videos on Youtube

Pat R Ellery
Author by

Pat R Ellery

#SOreadytohelp Share!

Updated on January 07, 2020

Comments

  • Pat R Ellery
    Pat R Ellery over 4 years

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

    • Gray
      Gray over 12 years
    • Alex S
      Alex S over 12 years
      They're not even comparable concepts. They just happen to be spelled similarly. Do some reading.
    • Ed Staub
      Ed Staub over 12 years
      They have nothing to do with each other - totally unrelated, conceptually. Study each separately.
    • Pat R Ellery
      Pat R Ellery over 12 years
      @Gray no, I found something about this and I was wondering what is the difference
    • David Titarenco
      David Titarenco over 12 years
      This should not be closed as it appears to be a relatively common question.
    • akappa
      akappa over 12 years
      voted to reopen due to a good answer, which may be useful for users who lands here.
  • sarnold
    sarnold over 12 years
    Welcome to Stack Overflow! While this article may in fact answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. Stack Overflow is only as useful as its questions and answers, and if this blog host goes down or URLs get moved around, this answer becomes useless. Thanks!
  • diaryfolio
    diaryfolio about 10 years
    better answer than most of Java books !!
  • datnt
    datnt almost 10 years
    Thank you. This answer is well explained in compare to others called them selves "experts".
  • Samarth Shah
    Samarth Shah over 8 years
    Good answer but the signature of finalize method is wrong.
  • Slaw
    Slaw over 4 years
    Note: The Object#finalize() method was deprecated in Java 9 (JDK-8165641).