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

Answer Posted / mukesh makwana

* 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 ?    14 Yes 5 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain covariant method overriding in java.

540


How can you add and remove nodes in jtree?

610


Why arraylist is not synchronized in java example?

465


What is bom encoding?

552


Define how objects are stored in java?

561






What is the difference between C++ and Java and your preferences?

607


What are the core java topics?

561


What is the difference between && and & in java?

567


Can a class be defined inside an interface?

559


Can interface be private in java?

540


What is keyword and identifier?

636


How can a gui component handle its own events?

591


What are 3 data types?

542


What are the differences between include directive and include action?

544


What is a lightweight component?

591