This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize()

 
Surendra Kumar
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Can some one explain me the design reason behind keeping java.lang.Object finalize() method as protected.

Thanks
Surendra
 
Pavan Kumat
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Programmer can customize the finialize() method of the class he's developing, In fact you are supposed to define all cleanup code (like relinquishing resources etc.) in a finaize() method though you are not really sure when it will get called

And since every class by default extends from Object class, finalize() got to have protected to get derived in sub-classes.
 
Surendra Kumar
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pavan,


Yes i agree with that, But to implement it it need not be protected , it can also be public like all other important operations in Object

For example equals(), hascode(), and toString().

I do not see any specific reason for it to be protected.

Cheers
Surendra
 
Pavan Kumat
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public methods are used when some other code is supposed to invoke the method. but finalize() is a callback method, means its called by GC thread before desroying an object. No other part of code ever calls finalize() method. Then why to make it public unnecessarily.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic