ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Finalize method in java

In this post we will learn about finalize() method. Few facts about finalize() method that we should be aware of are listed below:

  • It is a method of object() class and we can override and overload this method.
  • This method is called by garbage collector whenever object is removing from the memory by the garbage collector.
  • In order to execute some bunch of statements, garbage collector coming to heap for removing abandoned objects and it scans the heap in order to find out abandoned objects. Before all this process garbage collector call finalize() method every-time.
  • finalize() method do house keeping activity.
  • We can use finalize() method n number of times as finalize method is empty. Garbage Collector comes once in a while in memory management. It gets less priority because it is a daemon thread.

 

Let us go through simple lines of code to understand the usages of finalize() method.

  1. class{
  2.  
  3. @override
  4.  
  5. protected void finalize() throws Throwable {
  6.  
  7. System.out.println("from finalize " );
  8.  
  9. }
  10.  
  11. }
  12.  
  13. class Manager {
  14.  
  15. public static void main( String[] args) {
  16.  
  17. E e1 = new E();
  18.  
  19. e1=null;
  20.  
  21. System.gc();
  22.  
  23. System.out.println( " done" );
  24.  
  25. }
  26.  
  27. }
  28.  
  29. <span style="text-decoration: underline;">OUTPUT:</span>
  30.  
  31. done
  32.  
  33. from finalize

 

The order of above output may vary.

gc() is a static method inside System class. It is just a request not a command.

 

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!