Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

destroy() method

 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can call but remember that it container who should invoke the method. No you won't get exception if you call it.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Pradeep bhatt
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul

You are right!
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Joseph Hatton
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We should make a point to post interesting interview questions here so that it helps others. Mani you must be having lot of questions.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic