what are the differences between final,finally,finalize
methods?

Answer Posted / ramudu

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.

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are recursive functions?

577


How do you define a set in java?

519


What is an exception in java?

648


What is the purpose of using break in each case of switch statement?

568


Can we override protected method in java?

582






How do weakhashmap works?

593


Can we create a constructor in abstract class?

575


What is the use of math abs in java?

553


What are operators and its types?

579


What is the collections api?

579


why java does not support unsigned keyword?

4271


What are the uses of synchronized keyword?

558


What is loop in java?

537


explain the concept of virtual method invocation in polymorphism in detail?

1722


Write a function for palindrome and factorial and explain?

661