• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Garbage Collection

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why finalize() is declared protected?
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check this

javaranch
 
Niranjan Prasad
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 coderanch, 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.
reply
    Bookmark Topic Watch Topic
  • New Topic