What is final, finally and finalize ?

Answer : Final is used to apply limitations on class, method and variable…

What is final, finally and finalize ?

Final

  • Final is used to apply limitations on class, method and variable. Final variable value couldn’t be changed final method couldn’t be overridden and final class can’t be inherited.
  • Final is a keyword.

Sample Code

  • Once initialized value of variable cannot be changed.
[pastacode lang=”java” manual=”class%20A%20%0A%7B%20%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20int%20a%20%3D%205%3B%20%0A%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20final%20variable%20%0A%20%20%20%20%20%20%20%20final%20int%20b%20%3D%206%3B%20%0A%20%20%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20a%2B%2B%3B%20%0A%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20modifying%20the%20final%20variable%20%3A%20%20%0A%20%20%20%20%20%20%20%20b%2B%2B%3B%20%0A%20%20%20%20%7D%20%0A%7D%20″ message=”” highlight=”” provider=”manual”/]
  • If we declare any variable as final, we can’t change its contents since it is final, and if we change it then we have the Compile Time Error.

Finally

  • Finally is used to place significant code, whether exception is handled or not that time will be executed.
  • Finally is a block.

Sample Code

[pastacode lang=”java” manual=”%2F%2F%20A%20Java%20program%20to%20demonstrate%20finally.%20%0Aclass%20Wikitechy%0A%7B%20%0A%20%20%20%20%20%0A%20%20%20%20static%20void%20A()%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20try%20%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22inside%20A%22)%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20throw%20new%20RuntimeException(%22demo%22)%3B%20%0A%20%20%20%20%20%20%20%20%7D%20%0A%20%20%20%20%20%20%20%20finally%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22A’s%20finally%22)%3B%20%0A%20%20%20%20%20%20%20%20%7D%20%0A%20%20%20%20%7D%20%20%0A%20%20%20%20static%20void%20B()%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20try%20%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22inside%20B%22)%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%20%0A%20%20%20%20%20%20%20%20%7D%20%0A%20%20%20%20%20%20%20%20finally%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22B’s%20finally%22)%3B%20%0A%20%20%20%20%20%20%20%20%7D%20%0A%20%20%20%20%7D%20%0A%20%20%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20try%20%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20A()%3B%20%0A%20%20%20%20%20%20%20%20%7D%20%0A%20%20%20%20%20%20%20%20catch%20(Exception%20e)%20%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22Exception%20caught%22)%3B%20%0A%20%20%20%20%20%20%20%20%7D%20%0A%20%20%20%20%20%20%20%20B()%3B%20%0A%20%20%20%20%7D%20%0A%7D%20″ message=”” highlight=”” provider=”manual”/]

Output

inside A
A's finally
Exception caught
inside B
B's finally

Finalize

  • Finalize is using for the processing of clean up to be perform just before object is garbage collected.
  • Finalize is a method.
  • Once finalize method completes immediately Garbage Collector destroy that object.

Syntax

[pastacode lang=”java” manual=”protected%20void%20finalize%20throws%20Throwable%0A%7B%0A%0A%7D” message=”” highlight=”” provider=”manual”/]

Sample Code

[pastacode lang=”markup” manual=”class%20Wikitechy%20%0A%7B%20%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20String%20s%20%3D%20new%20String(%22RR%22)%3B%20%0A%20%20%20%20%20%20%20%20s%20%3D%20null%3B%20%0A%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Requesting%20JVM%20to%20call%20Garbage%20Collector%20method%20%0A%20%20%20%20%20%20%20%20System.gc()%3B%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Main%20Completes%22)%3B%20%0A%20%20%20%20%7D%20%0A%20%20%0A%20%20%20%20%2F%2F%20Here%20overriding%20finalize%20method%20%0A%20%20%20%20public%20void%20finalize()%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20System.out.println(%22finalize%20method%20overriden%22)%3B%20%0A%20%20%20%20%7D%20%0A%7D%20%0A” message=”” highlight=”” provider=”manual”/]

Output

Main Completes
Leave a Reply

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

You May Also Like