Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection?

 
raymond yadao
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.*;
public class test
{
TextField tf;
public void initComp()
{
Panel p = new Panel();
tf = new TextField();
p.add(tf);
}
public static void main(String args[])
{
Frame f = new Frame("test");
f.setVisible(true);
f.pack();
test t = new test();
f.add(test.initComp());
}
}
is Panel p in initComp() method eligible for garbage collection by the time the method exits?
raymond
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi raymond
ur prog will not compile ...
AW Panel p in initComp() method IS eligible for garbage collection by the time the method exits AS all local variable/refrence are elegible to be collected by garbage as stack has to be cleaned.
HTH
CMIW
 
raymond yadao
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.*;
public class garbage
{
TextField tf;
Panel mainPanel = new Panel();
public void initComp()
{
Panel p = new Panel();
tf = new TextField();
p.add(tf);
mainPanel.add(p);
}
public static void main(String args[])
{
Frame f = new Frame("test");
f.setVisible(true);
f.pack();
garbage t = new garbage();
t.initComp();
f.add(t.mainPanel);
}
}
sorry i put the wrong code. here's my new code and my question is: Is Panel p in initComp() method eligible for garbage collection as the method terminates?(the Panel p still holds the TextField f);
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no it is not, but not because the Panel p still holds the TextField f, but because mainpanel holds a reference to p and mainpanel is an instance member of class garbage...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic