Tuesday 12 April 2016

Difference between final, finally and finalize() in java

final 

1) final is a keyword in java.
2) final in java can be used with :

  • variable - If you mark a variable as final then once you initialize value to that variable, it can never be changed.
  • method - If you mark a method as final in your class then that method cannot be overridden by the sub-classes method.
  • class - If you mark a class as final in java then that class cannot be extended (or sub-classsed)


finally

1) finally is a block in java
2) finally block is used with try block in exception handling. This block will surely get executed irrespective of whether exception is handled or not. 

click to know more in detail about finally block


finalize()

1) finalize() is a method in java.
2) finalize() method is called at least once, before the object is garbage collected, to perform any cleanup activity like releasing any system resources if held or closing the open connection.



This is the main difference between final, finally and finalize().
Here we have talked about just difference but there is more to know about each one of them. To know in depth about working of each of them follow the given link above.

No comments:

Post a Comment

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.