I have read the thread What are the roots?, but it doesn't address my question. That thread basically explains under what condition is an object ready for garbage collection. The question here is whether the condition is met in certain circumstances, and if so, whether such behavior will lead to problems.
By the term active, I take to mean something like:
- a
Thread
object that hasn't been joined yet, and - a
JFrame
object that hasn't been closed yet (still having a visible GUI interface).
In the book Java: A Beginner's Guide, I frequently find code that creates some objects and then discards the only (apparent) reference to them while they are still active. Will this results in possible premature garbage collection and causes certain problems, or the runtime system actually has a reference to these active objects (thus the parenthesized apparent in the preceding text), so it is OK for the programmer to have none? Following are two examples:
1) Concerning Thread
:
{
Thread thrd = new Thread(...);
... // no thrd.join();
}
2) Concerning JFrame
:
class SwingDemo {
SwingDemo() {
JFrame jfrm = new JFrame("A Simple Swing Application");
...
jfrm.setVisible(true);
}
...
}