wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finalize method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finalize method" Watch "finalize method" New topic
Author

finalize method

Praveena khandavalli
Greenhorn

Joined: Jan 05, 2002
Posts: 21
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.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205

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 ]

Rob
SCJP 1.4
Raji Addepalli
Ranch Hand

Joined: Dec 05, 2001
Posts: 50
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.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finalize method