• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

garbage collection?

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
reply
    Bookmark Topic Watch Topic
  • New Topic