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.
Joined: Aug 15, 2004
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.
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
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
Joined: Dec 12, 2002
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