• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

finalize method

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone explain correct answer for this question?
Which of the following statements about finalize methods are incorrect?
[Check all correct answers]
A) The purpose of a finalize method is to recover memory and other system resources.
B) The purpose of a finalize method is to recover system resources other than memory.
C) You should always write a finalize method for every class.
D) The order in which objects are created controls the order in which their finalize methods are called.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A) The purpose of a finalize method is to recover memory and other system resources.
B) The purpose of a finalize method is to recover system resources other than memory.
C) You should always write a finalize method for every class.
D) The order in which objects are created controls the order in which their finalize methods are called.


A) seems incorrect because you don't really have to worry about memory allocated within the VM; that's what the gc is for, so you're getting this for free already
B) This is really the main reason for finalizers; In fact, my understanding is that they are really just hooks to let the VM interact with applications *outside* the VM...in "native" land, so that for example, if you are using a native library and are done with some object you created via that native library, the VM , via your finalizer, can clean up those outside resources.
C) HA, not even. I have yet to write a finalizer in production code.
D) Nope, you get no guarantees about the order of object deallocation, when finalizers get called, or even when the gc runs.
So the only correct statement here is B).
[ January 10, 2002: Message edited by: Rob Ross ]
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone explain correct answer for this question?
Which of the following statements about finalize methods are incorrect?
[Check all correct answers]
A) The purpose of a finalize method is to recover memory and other system resources.
B) The purpose of a finalize method is to recover system resources other than memory.
C) You should always write a finalize method for every class.
D) The order in which objects are created controls the order in which their finalize methods are called.
Answers :a,c,d
These three are incorrect statements for finilize.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answers given by the others are correct. One other point I'd like to make is that, since you have no direct control over garbage collection, you also have no control over when (or even if) a finalize method is executed. For example, if it is not warranted, garbage collection may not be run before your program terminates. If this is the case, the finalize methods on any objects in memory will not be executed.
I've found, in practice, the finalize method is very seldomly used.
Corey
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
just a points to keep in mind...
1. if we call finalize() explicitly does it garbage collect the object???
Ans: NO. GC does that job and calls finalize() when it wants to destroy the object memory.
2. where the exceptions thrown by finalize() is thrown and if we have called finalize() explicitly then do we need to handle exception thrown by finalize() method?
e.g.

Ans: its thrown to GC when called by GC but if we call it explicitly then it is treated like any other method throwing exception.
regards
maulin.
 
reply
    Bookmark Topic Watch Topic
  • New Topic