aspose file tools*
The moose likes Servlets and the fly likes destroy() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "destroy() method" Watch "destroy() method" New topic
Author

destroy() method

Grishma Dube
Ranch Hand

Joined: Jul 01, 2003
Posts: 275
Hi,

I came across this question during an interview, pls answer this question..

Can we call a destroy() method inside init() or service() method?

TIA

Grishma
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Yes you can call but remember that it container who should invoke the method. No you won't get exception if you call it.


Groovy
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
i tried it. and what i got is.

yes we can call destory() and it wouldn't give you any compile time error nor any runtime error. but it wouldn't work like we think even that call to detroy() didn't work at all.

let me know if someone gets something else.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Pradeep Bhat:
Yes you can call but remember that it container who should invoke the method. No you won't get exception if you call it.


thats why i haven't tried it before.

all the books, docs, tutorials, and specifications are crying out loud that the container invokes this method. anyways.
Joseph Hatton
Greenhorn

Joined: Dec 12, 2002
Posts: 20
That is kinda a weird questions because why would you want to call destroy() method when you are initializing objects? destroy() method is usually added to controlling and cleaning-up threads in an applet environment. There are 3 ways of handling destroy() method:

(1) Leave out the destroy(). If this method is left out of your code. You super class destroy() method is called.
(2) Add a destroy() method with your own clean up code. So when the applet is closed or exited, the overriding destroy() method is called.
(3) Add a destroy() method to start, stop, and destroy threads.

Hope that helps.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874

That is kinda a weird questions because why would you want to call destroy() method when you are initializing objects?


yes. it is weired. but it is just about we can or not.


destroy() method is usually added to controlling and cleaning-up threads in an applet environment.


couldn't get you here mate, really. why are you talking about the applet.
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
I agree that this is a strange question, but for the sake of curiosity, I tried it in a few different models just to see what happens. This is what I found (Mind you, I am making an educated guess as to the reasons this is happening, I could be wrong).

If you call destroy() from an init() or service method, the code that is in the destroy() will run just like any other method. But I don't think it actually destroys the servlet (or takes the servlet out of service). The API states
This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.
The destroy() method is called by the container when all of the servlet's threads have exited or it hit a time out. It is giving you a chance to clean up resources, not actually destroying the servlet. Since the method itself has nothing to do with the lifecycle of the servlet, you can call it from any other servlet method without exception.

Again, I may be mistaken, and please let me know if I am!


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Paul

You are right!
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Originally posted by Joseph Hatton:
That is kinda a weird questions because why would you want to call destroy() method when you are initializing objects?


It is weird. But I ask this question in interviews to see what people answer when they face such weird questions. Everybody knows that what is the use of destroy method and who will call it. But very few can think and respond when this question is asked. Some of the answers I have received for this question are
i) It won't work. It will give a compile time error
ii) It won't work. It will throw an exception at runtime.
iii) It will work. The Servlet will be destroyed when this method is called.
iv) The servlet engine will crash or hang
v)This is a weird question. Why would you want to call a destroy() method when you are initializing objects


Mani
Quaerendo Invenietis
Joseph Hatton
Greenhorn

Joined: Dec 12, 2002
Posts: 20
Hi, all!

The reason I said that it was weird is because I would NOT have given the correct answer. I am usually slow and methodical instead of fast and quick-witted! I have been to alot of inteviews where their goal it to trip you up instead of finding out what you know. If I had to truly anwser that question, I would respond like this: "I don't know that answer, but anyone that wanted to find the solution could go on the internet to look that up on http://www.javaranch.com

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

We should make a point to post interesting interview questions here so that it helps others. Mani you must be having lot of questions.
 
Consider Paul's rocket mass heater.
 
subject: destroy() method