Possible Duplicate:
Why is the finalize() method in java.lang.Object “protected”?
The finalize method is defined with a protected scope, then how is it possible that some other object such as the garbage collector is able to call it.
Possible Duplicate:
Why is the finalize() method in java.lang.Object “protected”?
The finalize method is defined with a protected scope, then how is it possible that some other object such as the garbage collector is able to call it.
Because the garbage collector is part of the language specification and thus can do whatever the language specification says it can.
protected is not the same as private, and the garbage collector could access it even if it were private.
Why is the finalize() method in java.lang.Object "protected"?
From the perspective of the Java Language Specification, the garbage collector is not an object, and is not governed by the normal access rules. It is "part of the JVM" and can do things that ordinary Java code can't do. (Indeed, it is probably not implemented as Java code at all!)
Think about it. If the GC was required to conform to the access rules, then it would not be able to access private
fields of objects during the mark phase, and therefore could not determine whether objects were reachable. In other words, it wouldn't be able to fulfil the JLS requirements for memory management.