what are the differences between final, finally, finalize in java?

final:
final is a keyword. The keyword “final” in Java is used in different ways depending on the context. We can have final methods, final
classes, final data members and final local variables. A final class implicitly has all the methods as final, but not necessarily the data members. A final class may not be extended; neither may a final method be overridden.

finally:
finally is a block. finally is block used in exception handling. Finally can be followed by try-catch or without catch block. But once we place a finally block then it will be executed always. 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 and used in garbage collection. finalize() method will be invoked just before the Object is garbage collected. It will be called automatically by the JVM on the basis of resource reallocating. Even programmers can call finalize method by using System.gc();

Leave comments if you have more information about the topic.

One Thought on “what are the differences between final, finally, finalize in java?

  1. Hasnen Laxmidhar on June 10, 2014 at 5:32 pm said:

    Why Finalize method protected ..?

Leave a Reply to Hasnen Laxmidhar Cancel 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> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation