• Post Reply Bookmark Topic Watch Topic
  • New Topic

Destructor equivalent wanted  RSS feed

 
Ketan KC Chachad
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just wanted to know if there is any way we can assure that a particular method will be invoked just before the object is destroyed by the garbage collector. I tried using a finalize() method but don't know if such a method exists. Actually there was this problem statement given by one of my teachers where we had to print the statements "Begin\nStart\nEnd" on the console but the main() method should just have the statement. We can enforce printing "Begin" by putting the print() statement in the constructor of the class, but I am confused as to how to make "End" print only and only when the object is destroyed / removed from memory. Please do help me in this regard.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it's like this...

http://www.faqs.org/docs/think_java/TIJ306.htm#Index356

and

http://www.faqs.org/docs/think_java/TIJ306.htm#Index360
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mothod finalize() exists, but it cannot be guaranted that it is called.

"begin" part should be easy (static is the key)

for the "end" part:
Print it when program ends (check the api of Runtime class) or when main thread exist (check api for Thread and find the right way to use it).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!