I declare and define an instance of JMenu in the
constructor of my JFrame-class. Then I use
setJMenu(JMenu) to attach the menu to the
main window. It works well, I can use the
menu, I do not experience strange behavior.
But somehow I am concerned that the JMenu
no longer exists after program flow leaves the
constructor. Does the garbage collector remove
it and is it mere change that I have a menu, or
does the call to setJMenu keep the instance alive?
//class Instrument
public class Instrument extends javax.swing.JFrame
{
//constructor
public Instrument()
{
//call parent constructor
super(a);
//menubar
javax.swing.JMenuBar menubar=new javax.swing.JMenuBar();
//add items to menubar
//set JMenuBar
setJMenuBar(menubar);
}
}
Roedy Green - 16 Dec 2005 09:29 GMT
On Fri, 16 Dec 2005 09:57:42 +0100, "Martijn Mulder"
<mbmulder@wanadoo.nl> wrote, quoted or indirectly quoted someone who
said :
>I am concerned that the JMenu
>no longer exists after program flow leaves the
>constructor.
You have added the JMenu to the JFrame. the JFrame has a reference to
the JMenu. Even after you drop all references to your JFrame, if it
is on screen Java still has a pointer to it. Only after you dispose
the JFrame and drop all your own references to it will everything be
GCed.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Chris Smith - 16 Dec 2005 18:44 GMT
> I declare and define an instance of JMenu in the
> constructor of my JFrame-class. Then I use
[quoted text clipped - 6 lines]
> it and is it mere change that I have a menu, or
> does the call to setJMenu keep the instance alive?
Roedy explained the mechanism. However, the more important response is
this. Trust the garbage collector not to get rid of anything you are
still using. Unless you use the java.lang.ref package, it is NEVER
possible for the garbage collector to dispose of an object that you
might be using for any purpose. If you care whether the object exists
or not, then the answer is that the garbage collector won't dispose of
it. Any time spent worrying about whether the garbage collector will
collect an object that you need to remain in existence is wasted time.
(Incidentally, there are a few gotchas in the other direction -- the
garbage collector keeping something that you don't need. These tend to
result from programming sloppiness, or the use of classloaders other
than the system one.)

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Roedy Green - 17 Dec 2005 03:12 GMT
>(Incidentally, there are a few gotchas in the other direction -- the
>garbage collector keeping something that you don't need. These tend to
>result from programming sloppiness, or the use of classloaders other
>than the system one.)
see http://mindprod.com/jgloss/packratting.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.