File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finalize Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finalize" Watch "finalize" New topic
Author

finalize

Sujittt Tripathyrr
Ranch Hand

Joined: Jun 21, 2006
Posts: 96
Hi

I know the concept of finalize in java but i want a program to simulate the concept.

Thanks in advance.

Sunil Kumar Gupta
Ranch Hand

Joined: Aug 26, 2005
Posts: 824
May be this can help you

finalize() example


Lack of will power has caused more failure than lack of intelligence or ability.
My Blog | Red5 Resources | Technology Update | Daily Technology Tips
Sujittt Tripathyrr
Ranch Hand

Joined: Jun 21, 2006
Posts: 96
public class Finalize
{

public void finalize()
{
System.out.println("Going for GC");
}


public static void main(String s[])
{
Finalize f1=new Finalize();
f1=null;
System.gc();

}
}

why here the finalize block is not executing.

Thanks
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Sujittt Tripathyrr:
public class Finalize
{

public void finalize()
{
System.out.println("Going for GC");
}


public static void main(String s[])
{
Finalize f1=new Finalize();
f1=null;
System.gc();

}
}

why here the finalize block is not executing.

Thanks


Well,

Don't mind but, have you ever tried to run this program.. It is running fine at my PC>


The Best way to predict your future is to create it - Every great individual common man
Akin Ola
Greenhorn

Joined: Jul 18, 2006
Posts: 4
d finalize() method as u put it in ur program works on my system )
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
System.gc() does not guarantee that the finalize() method will run. It all depends upon the underlying JVM implementation. So, it may work in some computers and not in others. It also depends upon how much memory has been used up by the JVM. If very less memory is left, then garbage collection 'can' be started by the JVM itself.

Please point out if I am wrong.

regards,
vijay.
Sujittt Tripathyrr
Ranch Hand

Joined: Jun 21, 2006
Posts: 96
How i will know that finalize block is executing because the "Going for GC" out put is not printing.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You cannot guarantee that the object will be garbage collected, if it is, then finalize() will be called. System.GC may or may not cause garbage collection, usually it does - but it is not certain. Sometimes it helps to call System.GC more than once - but you can never be certain that it has the effect you want.

By the way: the finalize() method in this code is public (it has been widened from protected access). If a subclass of this class wanted to use a protected finalize() method (as in Object's contract) it cannot do so.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Akin Ola:
d finalize() method as u put it in ur program works on my system )


Please Use Real Words in this forum, unless you want to perceived as illiterate. Thanks.

(Yes, I corrected a spelling mistake of my own )
[ July 19, 2006: Message edited by: Barry Gaunt ]
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023

Try this code. It prints out "Going for GC".
Sujittt Tripathyrr
Ranch Hand

Joined: Jun 21, 2006
Posts: 96
Hi

I executed the code in JDeveloper and no output it is displaying.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
I used eclipse and Java 5.0.
Sujittt Tripathyrr
Ranch Hand

Joined: Jun 21, 2006
Posts: 96
Hi wise

Just now i check in command prompt it is working fine and also printing "Going for GC".

Thanks for help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finalize