Win a copy of Rust Web Development this week in the Other Languages 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

finalize method-- W Brogden's Mock

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 4
Which of the following classes should have a custom finalize method?
a A class that creates a socket.
b All classes.
c Any class that extends a class having a finalize method.
d Any class that creates an array of primitive values.
Ansers are A, C
While I don't have problem with A.
But isn't C suggesting that any class that is extended from Object should also declare finalize method becuase Object declares finalaize method?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I think you are logically correct. But (from the API):


The finalize method of class Object performs no special action; it simply returns normally.


I would have interpreted that option to have meant that some intermediate base class had provided a non trivial implementation of finalize, so I would have chosen C.
But why? Surely the finalize method is called polymorphically through a reference to an Object?
So the lowest finalize in the hierachy will be called. Provided that finalize called its super.finalize() method, there is no need to provide an implementation in every subclass all the way down the hierachy.
For example, if we have a hierachy: Object <- A <- B <- C <- D, and B implements finalize. Then B must call super.finalize(). C and D do not need to implement finalize.
So choice C looks not correct, but not for the reason Sarma proposed. Bill?
I found this description (near the end )
[ March 15, 2003: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not convinced that one should have a custom finalize() method in a class that creates a socket.
I understand that finalize() can be used to clean up non-memory resources used by an object. And socket() uses a non-memory resource. However�
Read Item #6: Avoid finalizers in Effective Java by Joshua Bloch.
The Java Programming Language says �You should only rarely need to write a finalize() method�. I�ll bet people use socket() frequently rather than rarely.
In Java Network Programming by Harold (O�Reilly) there is no discussion on finalize(). In Java RMI by Grosso (O�Reilly) there is no discussion on finalize(). In Core Java II there is no discussion on finalize().
I searched the Sun Java Forums and found no advice on using finalize() when using socket(). What I gleaned from scanning the Sun Java Forums for use of the word "finalize" is that nowadays people recommend using weak references and advise against using finalize().
I am tempted to post a question on the Sun Java Advanced Features forum and ask whether people who do socket() calls for a living ever use finalize().
[ March 16, 2003: Message edited by: Marlene Miller ]
[ March 16, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I had to answer Question 4 above on an exam, I couldn't honestly answer A or C. C - for the same reason that Sarma gives. I would have to skip the question and get it wrong, I suppose.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I was trying to get at with that question is the need to write a finalize for classes that use resources which are not cleaned up normally by the GC process. I think an object that may have an open socket is a good candidate, but I am open to other suggestions.
Bill
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you William for your insight on this question.
I noticed when I was searching the Sun Java forums for discussions on finalize that some of the participants with a lot of Java knowledge and experience do not understand finalize is used to clean up non-memory resources (as opposed to memory resources). This is a good point to test.
I also noticed when searching the Sun Java Technical reports and Effective Java, an even more important thing to know about finalize is that an explicit termination method (such as close) used in a try-finally block is better than finalize. Finalize is a safety net in case the owner of the object forgets to call the explicit termination method.
My only suggestion or request is that the correct answer to a question agrees with the real world.
 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene,
Your research on this issue is outstanding. I have seen very few people who really went in grate depths to answer a question. Thanks. This really inspired me.
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, this is a great discussion, but I just wanted to point out that you will not have a question like this on the real exam. You *can* expect questions about how/when finalize() works, but not about whether it's a good idea
cheers,
Kathy
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Kathy. That is good news.
 
reply
    Bookmark Topic Watch Topic
  • New Topic