File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes finalize method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "finalize method" Watch "finalize method" New topic

finalize method

kamal vashist

Joined: Sep 13, 2006
Posts: 2
Why there is protected keyword before finalize() method.
Waiting for prompt reply.
Thank you
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

protected is a valid access modifier, regardless of how a method is decorated.

Is there a particular method you're concerned about?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


Welcome to JavaRanch!

A "protected" method can only be called by subclasses of the defining class, or other methods in the same package. It's not perfect, but here it's being used to suggest that it's OK (actually encouraged) for you to call super.finalize() when you're overriding finalize(), but you shouldn't ever need to call finalize() on some other object. Only the garbage collector is supposed to call it. The "protected" therefore prevents you from making calls you shouldn't make.

[Jess in Action][AskingGoodQuestions]
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257


The protected method makes only the same and sub type object can call the finalize, and all other can't call those and as Ernest told only GC is supposed to call it.
I agree. Here's the link:
subject: finalize method
It's not a secret anymore!