top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is 'finalize()' method in Java, why is it used and what could have been the problems if this method was not there?

0 votes
630 views
What is 'finalize()' method in Java, why is it used and what could have been the problems if this method was not there?
posted Jul 13, 2017 by Rohini Dv

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Finalize is a method.Finalize is used to perform clean up processing just before object is garbage collected.
The finalize() method is invoked each time before the object is garbage collected. This method can be used to perform cleanup processing. This method is defined in Object class as.

public class TestGarbage1{  
 public void finalize(){System.out.println("object is garbage collected");}  
 public static void main(String args[]){  
  TestGarbage1 s1=new TestGarbage1();  
  TestGarbage1 s2=new TestGarbage1();  
  s1=null;  
  s2=null;  
  System.gc();  
 }  
}
answer Jul 14, 2017 by Ajay Kumar
...