The Artima Developer Community
Sponsored Link

Java Buzz Forum
Finalize() method in java with example program

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Finalize() method in java with example program Posted: Mar 19, 2017 3:25 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Finalize() method in java with example program
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • finalize() method pre defined method which is present in java.lang.Object class.
  • finalize() method is protected  method defined in java.lang.Object class.


  1. protected void finalize() throws Throwable{
  2.  
  3. }



1.What is purpose of overriding finalize() method?

  • The finalize() method should be overridden for an object to include the clean up code or to dispose of the system resources that should to be done before the object is garbage collected.

2.How many times does the garbage collector calls the finalize() method for an object? 

  • Only once.

3.What happens if an uncaught exception is thrown from during the execution of finalize() method of  an object?

  •  The exception will be ignored and the garbage collection (finalization) of that object terminates

  •  If we are overriding finalize() method then its our responsibility to call finalize() method explicitly.

  •  finalize() method never invoked more than once by JVM or any given object.
  • There is no guaranty that if we call finalize() method but we can force garbage collector by calling below two methods
  • System.gc();
  • Runtime.getRuntime().gc();

#1 : Java program to explain about finalize() method


  1. package inheritance
  2. public class B {
  3.  /**
  4.  * Finalize() method in java
  5.  * @author www.instanceofjava.com
  6.  */
  7.   @Override
  8.   protected void finalize() throws Throwable {
  9.             try{
  10.                 System.out.println("Inside Finalize() method of Sub Class : B");
  11.             }catch(Throwable t){
  12.                 throw t;
  13.             }finally{
  14.                 System.out.println("Calling finalize() method of Super Class:  Object");
  15.                 super.finalize();
  16.             }
  17.          
  18.  }
  19.  
  20. public static void main(String[] args) throws Throwable{
  21.         B obj= new B();
  22.         String str=new String("finalize method in java");
  23.         str=null;
  24.         obj.finalize();
  25.         
  26.         }
  27. }

finalize() method in java with example program

Read: Finalize() method in java with example program

Topic: Becoming an Agile Leader, Part 3: How to Create Allies Previous Topic   Next Topic Topic: Becoming an Agile Leader, Part 2: Who to Approach

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use