{"id":460,"date":"2010-07-01T11:52:51","date_gmt":"2010-07-01T06:22:51","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=460"},"modified":"2010-07-01T11:52:51","modified_gmt":"2010-07-01T06:22:51","slug":"java-destructor-finalize","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-destructor-finalize\/","title":{"rendered":"Java Destructor &#8211; finalize()"},"content":{"rendered":"<figure id=\"attachment_461\" aria-describedby=\"caption-attachment-461\" style=\"width: 128px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/Trash.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-461\" title=\"Java Destructor finalize()\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/Trash.png?resize=128%2C128&#038;ssl=1\" alt=\"Java Destructor finalize()\" width=\"128\" height=\"128\" \/><\/a><figcaption id=\"caption-attachment-461\" class=\"wp-caption-text\">Java Destructor finalize()<\/figcaption><\/figure>\n<p>The aim of destructor\u00a0in any OOPs language is:<\/p>\n<ol>\n<li>Free up the memory (c++ suffer from memory allocation \/ deallocation)<\/li>\n<li>Clean up any other resources (like closing of open file stream)<\/li>\n<\/ol>\n<p>Java take cares of all and hence there is no destructor in Java. (With the help of Garbage collection)<br \/>\nbut still if you want to perform some additional tasks, then you can use the <strong>finalize()<\/strong> method of java.<\/p>\n<p>But, we can&#8217;t rely on\u00a0<tt>finalize()<\/tt> as it depends on GC. Sometimes, GC may never be invoked during the lifetime of the program. A good idea is to implement a method, say\u00a0<tt>cleanUp()<\/tt> which does, any special processing required, ofcourse, other than freeing up memory. The programmer should call this method at the approppriate place in the program. That is the only way to make sure your program works correctly.<\/p>\n<p>Example code of finalize in java:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nclass Thing {\n\n\tpublic static int number_of_things = 0;\n\tpublic String what;\n\n\tpublic Thing (String what) {\n\t        this.what = what;\n\t        number_of_things++;\n\t        }\n\n\tprotected void finalize ()  {\n\t        number_of_things--;\n\t        }\n\t}\n\npublic class TestDestructor {\n\tpublic static void main(String&#x5B;] args)\n\t{\n\t\tThing obj = new Thing(&quot;Test App&quot;);\n\n\t}\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Concept of destructor in JAVA. finalize() method.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[329],"class_list":["post-460","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":979,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/invoke-method-using-reflection-in-java\/","url_meta":{"origin":460,"position":0},"title":"Invoke method using reflection in JAVA","author":"Jitendra","date":"September 6, 2010","format":false,"excerpt":"Invoke method using reflection in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":536,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/immutable-nature-of-string-java\/","url_meta":{"origin":460,"position":1},"title":"Immutable nature of String &#8211; JAVA","author":"Jitendra","date":"July 5, 2010","format":false,"excerpt":"Immutable nature of String - JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Create instance of string in JAVA","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/String-1.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1937,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/virtual-function-in-java\/","url_meta":{"origin":460,"position":2},"title":"Virtual Function in JAVA","author":"Jitendra","date":"April 8, 2011","format":false,"excerpt":"Example and explanation of Virtual Function in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1655,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/introduction-to-annotation-in-java\/","url_meta":{"origin":460,"position":3},"title":"Introduction to Annotation in JAVA","author":"Jitendra","date":"March 14, 2011","format":false,"excerpt":"Introduction to Annotation in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3556,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/create-excel-file-in-java-using-apache-poi-library\/","url_meta":{"origin":460,"position":4},"title":"Create Excel File in Java using Apache POI Library","author":"Jitendra","date":"October 15, 2013","format":false,"excerpt":"Recently, I came across requirement to create ExcelSheet from thin Java Client used by Salesforce. So, I though to share my experience on Creating Excel Sheet in Java. As we know that Java is product of Oracle and Excel is product of Microsoft. Off-course, There will be no standard functionality\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Create Excel File in Java","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/Create-Excel-File-in-Java-300x166.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1712,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/life-cycle-of-jsp-tag-interface\/","url_meta":{"origin":460,"position":5},"title":"Life cycle of JSP Tag interface","author":"Jitendra","date":"March 17, 2011","format":false,"excerpt":"Life cycle of JSP Tag interface in Java","rel":"","context":"In &quot;JSP&quot;","block_context":{"text":"JSP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/jsp\/"},"img":{"alt_text":"Life Cycle of Tag Interface","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Life-Cycle-of-Tag-Interface.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=460"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/460\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}