• Post Reply Bookmark Topic Watch Topic
  • New Topic

Call a method when object dies  RSS feed

 
Harish Shivaraj
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.

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?

Thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harish Shivaraj wrote:Such as destructor (I know java doesn't have a destructor method). Is there something similar to compensate?


From your code snippet, you do know that you are returning (a reference to) the object right? The object will still be reachable, and hence, not "dead".

Even if you were using C++, which has destructors, since you are returning the object, and not deleting it, the destructor will not be running regardless.

Henry
 
Harish Shivaraj
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...


Henry
 
Harish Shivaraj
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harish Shivaraj
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry.
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.
 
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afternoon, Harish

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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!