As I understand it, only subclasses and classes in the same package can access a protected method. If that's true then how does the Garbage Collector call an object's finalize() method when the signature is "protected void finalize() throws Throwable" ? ------------------ TANSTAAFL
the call to System.gc() is transfered to Runtime.getRuntime().gc(); Which is a native function implemented in the JVM. In the JVM, you have a set of "garbage collectable" objects which gc and then if they are not finilized already, a call is made to the finalization. Thus we are not more in Java itself but in a lower level.
The garbage collector is not a class. It does not have to live by the rules of classes, including access modifiers. It is a part of the JVM itself. like the bootstrap classloader. It calls native code to do it's dirty work for it. The System.gc() method that users can call is NOT actually the garbage collector. It is a method that notifies that gc that now would be a good time to run. Native code can not get at classes without going through JNI which enforces access restrictions. [This message has been edited by Cindy Glass (edited May 23, 2001).]
"JavaRanch, where the deer and the Certified play" - David O'Meara
first of all, it is important to clear that with a PROTECTED modifier, the SUB-classes of a different package can also access the method. the garbage collector is a separate thread running at the backgroung. to be precise its a DAEMON thread. at what time it will garbage collect the waste objects, its unpredictable!!
Joined: May 08, 2001
Thanks, Cindy. You've hit the nail on the head. I fell into the trap of thinking that since the GC is part of Java it must obey the same rules that our classes do. ------------------ TANSTAAFL