Is there a way to call class method when is an objects dies. Take for instance
what I want is; log the response object before it dies. Now I want the logging process to be an implicit part of the code rather than explicit.
I could call the log method when the object is created. Which was easier. However, when the object is created not all attributes are set and therefore logging then makes no sense.
From the above code, by the time I return the object I have the state of the object complete and therefore it make sense to log the object then. The one obvious way to do this to just call the log method just before returning.
But is there a better design than that. Such as destructor (I know java doesn't have a destructor method). Is there something similar to compensate?
Harish Shivaraj wrote:Actually your right. The object at the point of return is still alive and not dead. Perhaps the question is really that, is there a way I could invoke the class method when the object is returned?
Well, your code snippet, in your original post, should work fine. What is wrong with that?
However, if it is just a code snippet, meaning it is much more complex than that ... and you really want to call the method after the processing for return has completed, then I guess you can do something like this...
Just to clarify that, its good enough for me if method get called just before it dies is still valid. However from my code snippet its probably not clear. But the over all intention is ofcourse that I would like to log the object close to the timeline of object death.
The current design i have is that, I call the .log method manually. I wondered if there was some way (design) that the method can be called automatically when the object is either returned or about die. If Java controller has some way to invoke it.
I suppose your post sort of concludes the "finally" is the way to go forward.
Harish Shivaraj wrote: But the over all intention is ofcourse that I would like to log the object close to the timeline of object death.
Well, if it is possible for you to determine the point of death. In C++, this would be the point where the delete operator is called -- and hence, the destructor is called. Then go to that point of death, and add a call to the log() method.
If however, it is not possible for you to determine the point of death, then obviously, there is no way the JVM can do it either. At best, you can either use the finalizer() method, which is called at the point of garbage collection; or use the Java shutdown hook, which is called at the point of JVM shutdown; or a combination of both.
Harish Shivaraj wrote:Perhaps the question is really that, is there a way I could invoke the class method when the object is returned?
Sounds like you might want to look into Aspect Oriented Programming in Java (AOP). For example, you can define aspects that are executed on entry to a method and on exit from a method. In your case, an aspect that is executed on exit from a method might be exactly what you are looking to do.
What do you mean by an object dying? It is not a term in common use. Henry is right; you can use a shutdown hook, which sounds very complicated but is actually quite easy to do, which will do something whenever the JVM exits (beware: don't use System.exit and make sure the shutdown hook can complete in a short time). That doesn't necessarily find all the reachable or unreachable objects. Or you can use finalize(), which sound very easy but is actually probably complicated. Remember there is no guarantee that finalize() will ever be called; it is only called when the object is deleted by the garbage collector. So you will be tempted to use System#runFinalizersOnExit() (or its counterpart in the Runtime class), and that can really mess your application up good and proper.
None of those techniques will reliably run code on every object which becomes unreachable, and I can't think of a way to do that.
Running finalizers by calling System.gc() repeatedly just before the JVM exits is probably the nearest approximation, but it probably can't be put into a shutdown hook because it would take too long. Also, read what Joshua Bloch says about the finalize() method in Effective Java (chapter 3). Is running code on unreachable objects really good object‑oriented practice?
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop