• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collector magic

 
Burkhardt Hufnagel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Laurent Leonard
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

------------------
Laurent Leonard
Laurent.Leonard@belgique.com
 
Burkhardt Hufnagel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh. Wait-a- minute... Your answer implies that native methods are not restricted by the access modifiers. That sounds like a Bad Thing.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
NIKHIL JAIN
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
Burkhardt Hufnagel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic