Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there C++ destructor equivalent in Java? [duplicate]

In simplest form, following is the design:

class Session {
  Timer t = new Timer();
  // ...
};

Whenever, Session is allocated, I start a timer inside it; the timer will be expired after 10-20 mins. Now, suppose if Session is destroyed before the timer can expire; then it's a scenario where I must stop timer. I don't know if is there any last method which is always called when Session is destroyed.

Is there some sort of C++ destructor equivalent in Java, which help me to cancel() the timer when Session is destroyed ? (without waiting for GC)

Edit: Please don't retag for C++. I wanted something of that equivalent. The Session is a phone session which is get destroyed when all the user connected to it are disconnected. Now, there is no Session method which is called lastly nor there is any exception.

like image 741
iammilind Avatar asked Jul 05 '11 09:07

iammilind


4 Answers

No, there's no such thing built into Java. The closest is to write a finalizer, but there's no guarantee that it'll run.

How is a Session destroyed? If there's a method called, by all means put the code there.

How do you create a Session? If you do it within a try/catch block, you can clean up everything in a finally block.

I would write a close() method that took care of it and call it in a finally block.

like image 199
duffymo Avatar answered Oct 28 '22 09:10

duffymo


Unlike C++, you cannot control object deallocation in Java - that is, the GC collects objects whenever it sees fit, merely guaranteeing that it won't collect anything that can be reached through any reference that is in scope at a given time.

There is Object.finalize(), which acts as a last-chance hook to do cleanup, but the runtime does not guarantee that it gets called at all.

I'd rethink the design and try to come up with a more explicit way of cleaning up your timers.

like image 21
tdammers Avatar answered Oct 28 '22 10:10

tdammers


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.

like image 29
Rupok Avatar answered Oct 28 '22 11:10

Rupok


First, finalize is not the equivalent of a destructor. Don't try and use it as one; it won't work. There is nothing built into Java for this, but in some circles, there is a convention to use void dispose() for this; instead of delete ptr;, you write ptr.dispose(). (The memory, of course, gets reclaimed later.) In such cases, it's a good idea to also define finalize, to generate some sort of internal error if the object is reclaimed before dispose is called.

like image 39
James Kanze Avatar answered Oct 28 '22 11:10

James Kanze