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

difference between destroy() & finalize()

Vishal Dhingreja
Greenhorn

Joined: Nov 19, 2004
Posts: 15
Hi All

What is the difference between destroy() method of the ServletAPI & finalize() method of core java api's.

On the very face of it they appear to be one & teh same. Is the only difference that one we can call from our code(destroy) where as we cannot control the execution of finalize method

Do post your comments on the same

Rgrds
Vishal
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
You should NOT be calling the destroy method in the servlet interface. Read the JavaDocs - destroy is called by the servlet container when a servlet instance is being taken out of service. Where do you people get these bizarre ideas?
You should review the JavaDocs to understand which methods exist to be called by the servlet container.

The obvious parallel is with finalize, which should ALSO not be called by your program code, which is called by the GC process.

If your design needs object cleanup code that you want to call, make your own method.
Bill
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
Originally posted by William Brogden:
Where do you people get these bizarre ideas?

Bill, He gets those ideas from the multiple-choice test you are doing for him.
--Dale--
Vishal Dhingreja
Greenhorn

Joined: Nov 19, 2004
Posts: 15
Hi William & Dale

To reply from where do I get those bizarre ideas . Well these are the interview questions put in top IT firms ( for newbies with limited experience).

These questions are not one off stray cases but a lot of interview candidates like me encounter them (before you brand those interviews as bizarre dumb fellas).

What I intended to ask was whether teh above mentioned methods can be invoked from developer code (now invoking it is bizarre idea is totally another thing)

Sorry for making that so very convoluted.

Nothing is more important than clearing an interview & getting selected & even if that means learning to answer & knowing BIZARRE questions.

Very Truly Yours
Vishal
Aparna Yadav
Greenhorn

Joined: Sep 13, 2004
Posts: 4
Hi Wiliam,
I know that Servlet's destroy() is called by the servlet container to indicate that the servlet is being taken out of service.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.

But then with this method we have got Object's finalize() method in the Servlet class.and it does the same job of releasing non mem resources.
So now can u pls tell in this when exactly destroy & finalize will be called & in which sequence.
And can i have both the methods implemented in my class then what code i shld write it in that.

Waiting for reply.
Thanks
Aparna.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
You can tell I am out of touch with the kind of interview questions you may get asked - I guess you truly have to be ready for anything, even really bizarre ideas.

Can destroy and finalize be addressed from developer code? Sure, why not - there is nothing magic about the servlet environment that would keep you from doing it.

Since destroy is the method that the servlet API requires, you should use it. The finalize method should be regarded as the absolute last chance to clean up as an object is removed from memory, and not the normal routine cleanup method. It is especially important for classes that may tie up operating system resources. Look at the way finalize is used in the java.io.FileInputStream class for example.

Naturally, since destroy is called while the servlet container still has a reference to the servlet object, it will be called before the GC process ever gets around to calling finalize. You can rely on destroy being called at a given point in the servlet class life-cycle but you can't rely on when finalize will be called, so I would put all cleanup code in destroy.

Bill
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: difference between destroy() & finalize()