File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finalize() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finalize() " Watch "finalize() " New topic
Author

finalize()

Surendra Kumar
Greenhorn

Joined: Apr 20, 2003
Posts: 21
Hello,

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

Thanks
Surendra
Pavan Kumat
Greenhorn

Joined: Dec 13, 2004
Posts: 10
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

Joined: Apr 20, 2003
Posts: 21
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

Joined: Dec 13, 2004
Posts: 10
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: finalize()