difference between final finally and finalize in java
Answers
Answered by
0
final- final keyword can be used with a class, variable or a method.
Finally- finally keyword is used with try-catch block for handling exception. The finally block is optional in try-catch block. The finally code block is always executed after try or catch block is completed. The general use case for finally block is to close the resources or clean up objects usedin try block. For e.g. Closing a FileStream, I/O stream objects, Database connections, HTTP connections are generally closed in a finally block.
Finalize()- This is the method of Object class. It isinvoked 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 thefinalize method.
Finally- finally keyword is used with try-catch block for handling exception. The finally block is optional in try-catch block. The finally code block is always executed after try or catch block is completed. The general use case for finally block is to close the resources or clean up objects usedin try block. For e.g. Closing a FileStream, I/O stream objects, Database connections, HTTP connections are generally closed in a finally block.
Finalize()- This is the method of Object class. It isinvoked 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 thefinalize method.
Answered by
2
Similar questions