@Someone You Know said:
[quote user="Renan "C#" Sousa"]@Someone You Know said:
@smxlong said:How can this code even be used? The constructor of MoreSpecificWorker takes an instance of a MoreSpecificWorker. How would that instance get constructed, since you already need to have an instance in order to create an instance?
Possibly there's another constructor that we're not seeing here — maybe one that passes this as a parameter, as Bulb suggested. Though I'm curious what the JVM garbage collector would do with an object that
has itself as a member variable. It's possible you might have to
explicitly set the "worker" member variable to null before this thing
would be garbage collected.
You could also pass null as the constructor parameter, though I'm not clear on what purpose the whole thing would serve in that case. Then again, I'm not clear on what purpose the whole thing serves to begin with.
Can't the garbage collector get around circular references, or at least self-references?
Actually, now that I think about it, you're right. It would only really be a problem if the garbage collector were using reference counting, which the
Sun Oracle JVM's garbage collector does not. I think it uses some kind of system where it determines whether an object is "reachable", via a chain of references, from some Kevin-Bacon-like object(s) it considers to always be reachable.
Generally, those Kevin-Bacon-like objects are classes and static members, plus anything in any stack (plus a few others):