This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Niranjan Prasad
Ranch Hand

Joined: Aug 13, 2005
Posts: 62
Why finalize() is declared protected?
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
check this

javaranch
Niranjan Prasad
Ranch Hand

Joined: Aug 13, 2005
Posts: 62
Can u plse tell me in more detail y it is not public and y it is
protected.

I have read the explanation given,but i was still unclear.
Every method is invoked by the JVM on the relevant object.
Here also finalize() is invoked by JVM on relevant object.

Niranjan
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
hi Niranjan

Did you study Edwin Dalorzo post it clearly tells why finalize method is protected


The finalize method is intended to be executed by the JVM just before GC collects garbage.

What is the minimum accessibility you could give to a method so that it could be overriden by any other sublass of object?

The answer is: protected.

You can override the method and do it public if you want, however, according to the contract of the finalize method, it would not make any sense.

A method is public if you pretend to offer some service through it, but no other class or object should invoke finalize directly. It should only be invoked by JVM before garbage collection.

Then, the best you can do is declare it protected.

Why?, well, If you do it private, it cannot be overriden, if you do it package accessible it cannot be overriden by classes in other packages. If you do it public, well, we already talked about it.

Then the best option is to make it protected.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Garbage Collection