Difference between final, finally and finalize in Java?
I am intermediate programmer in the programming world. Right now i am learning the programming language like C++, C sharp, Core Java, SQL and HTML. This days, i am working on Core Java and i have doubt that what is difference between the final, finally and finalize? Please help me.
Re: Difference between final, finally and finalize in Java?
I think this three method/functions are not that hard to understand and yes, they all are different from each other. I would say that "final" is known to be a keyword, which is used to declare constants. The "finally" block is used mainly in the exception handling. And the "finalize" method is usually called by garbage collector, in order to remove/throwout the object from memory if it no longer has been used. Hope i have clear your doubt.
Re: Difference between final, finally and finalize in Java?
The final is a keyword and used for making a member/variable as a constant which will not be modified once declared. The finally is a method and used during the exception handling to release any resource hold by try block. And the finalize method, all the resources mention will be garbage collected as soon as GC is called. The finalize and finally seems to be similar, but the difference is finally is gone execute every-time whether you call it or not, and this not similar with finalize.
Re: Difference between final, finally and finalize in Java?
According to me, all the three final, finally and finalize are different from each other and plays very important role in Java. The final is used for constant declaration, the finally is used in the exception handling and the finalize is used in the garbage collection. The finally and finalize has similar action of releasing the resources, but the difference is that finally will execute compulsorily and finalize will not. Hope your doubt would be cleared now.
Re: Difference between final, finally and finalize in Java?
Final: The final keyword is used for making variable as a constant.
Finally: The finalize method are used to handle exception. The finally block is known to optional and it provides a mechanism to clean up without regarding of what happens inside the try block.
Finalize: The finalize method provide help in the garbage collection. This method is call before any object is discarded by the garbage collector, which will allow it to clean up its state.
Re: Difference between final, finally and finalize in Java?
Finalize: This method is used for garbage collection. This method will get execute before Garbage Collection is done, because it release the resource which are hold by the program.
Finally: This method is used for in the exception handling mechanism. Once try block is executed, then finally block is must to execute. This block will release the resource hold by the try block.
Final: The final keyword is used declaring variables as constant.
Hope your doubt would be clear after this answer. All the best.
Re: Difference between final, finally and finalize in Java?
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.
final – constant declaration.