This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Is there a Destructor equivalent in JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is there a Destructor equivalent in JAVA" Watch "Is there a Destructor equivalent in JAVA" New topic
Author

Is there a Destructor equivalent in JAVA

sivakumar k r
Greenhorn

Joined: Jan 17, 2007
Posts: 4
Hi,

I truely understand that there is no need for a destructor in a java class since memory is automatically freed by the garbage collector. I would like to just see some display messages as and when an object actually goes out of scope or gets destroyed.

Is there any method that would perform this kind of an activity in java. Please keep me posted, your efforts are appreciated.


Shiv.. as I know him
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
Check out finalize in Object. Don't depend on this method though, as there are numerous situations in which it will not be called.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Note that in Java, variables do not directly represent objects like they do in C++ - variables in Java are references to objects. So an object cannot go out of scope - only a variable that refers to an object can go out of scope.

You can override finalize(), which is a method in class Object that is called by the garbage collector when it is about to permanently discard an object, but that's not exactly the same as a destructor. See the API documentation of the finalize() method in class Object.
[ February 07, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Is there a Destructor equivalent in JAVA