• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

finalize and Garbage Collection

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. From mughal's book
Which of these statements are true?
Select all valid answers
a> The compiler will fail to compile code that explicitly tries to call the finalize() method.
b> The finalize method should be declared with protected accessibility.
c> An overriding finalize method in any class can always throw checked exceptions.
d> The compiler will allow code that overloads the finalize method name.
e> The body of finalize method can only access other objects that are eligible for garbage collection.
I think the answer should be c> and d>.
But Mughal book says the answer is only d>.
Why isnt c> also a valid answer?
Signature of finalize in the object class itself says "throws Throwable" so why cant my class override finalize method and and throw an exception??
Checked, or unchecked shouldnt matter since the signature says "throws Throwable".
Infact, i should be able to throw Errors also.
2. The finalize() description in Jdk api docs says:
---------------------
If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.
....
Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

---------------------
What does all this mean? At the end of the day, if finalize() method in my class throws a checked exception, will the object be ever discarded or no? :roll:
Thank you,
Amol
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you that c is also a valid answer. Perhaps the question was implying that you couldn't throw a checked exception because it will be ignored, but that's stretching the interpretation if that's what they're saying.


At the end of the day, if finalize() method in my class throws a checked exception, will the object be ever discarded or no?


Yes it will. The exception is just ignored. The method terminates because that's just what happens when you throw an exception....it's like when you execute a return statement...the method is over at that point and no other code in the method will run.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this discussion:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=009358
[ March 31, 2002: Message edited by: Thomas Paul ]
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think the answer should be c> and d>.
But Mughal book says the answer is only d>.
Why isnt c> also a valid answer?
Signature of finalize in the object class itself says "throws Throwable" so why cant my class override finalize method and and throw an exception??
Checked, or unchecked shouldnt matter since the signature says "throws Throwable".
Infact, i should be able to throw Errors also.


Ha! I remember that question very well, and I would have agreed with you then. But Mughal's explanation is correct if somewhat mindbending.
Okay class A has finalize() throws Throwable.
Class B extends A and overridden finalize() throws IOException.
Class C extends B and overridden finalize() throws nada
So far so good.
Class D extends C and overriden finalize() wants to throw InterruptedException but can't
since D is now restricted to honor the signature of the finalize() of C... Tough break poor guy can't even throw IOException now if it wanted to...
But anyway IMO Mughal is still the best certification book ever.
-anthony
[ March 31, 2002: Message edited by: Anthony Villanueva ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Class B extends A and overridden finalize() throws IOException.


HEY no fair! He never said anything about an intermediate declaration that overrode the throws clause!! C is not correct if this is the case.
I'm taking my answer back.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,
over-riding doesn't imply one level of
inheritance. I completely agree though, I would
have incorrectly answered it as well.
Thankfully, a question like this will have one
correct answer in the exam and choice d) would
seem to be the strongest.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of those shady interpretation questions. If you're overridding Object's finalize() method, then it's true to say your overridden method can throw any Exception. If you manually reduce the exceptions that can be thrown in your overriden method's throws declaration, then overridden versions of that method will be restricted in what they can throw. But in order to be able to say that an overridden version of finalize() cannot throw *any* exception, you have to assume you are overriding an already overridden version of finalize(), AND some previously overridden version changed the throws clause.
That seems like a lot to assume to answer a question.
 
You get good luck from rubbing the belly of a 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