• 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

Is it a source of bug?

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is a piece of code




My question is that the reference b is assigned Button object three times...and the first two buttons "Hello" and "OK" are not referenced by any references..
Now as they are not referenced by any reference, so garbage collector may collects its memory, resulting in loss of these two buttons.....
Am I right or not???

Please comment......
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try running your code? How many buttons do you see on the UI. What happens when you click the buttons?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you *sure* nothing references them? Does anything *else* reference them?
 
Inder Kumar Rathore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it shows three buttons.....and yes there is no reference for first two buttons....

for your simplicity run this one....

import java.awt.*;
import java.awt.event.*;

public class Activator {
public static void main(String[] args) {
Button b;
Frame f = new Frame("Hello Java");
f.add(b = new Button("Hello"),BorderLayout.NORTH);
f.add(b = new Button("OK"), BorderLayout.CENTER);
f.add(b = new Button("Quit"),BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
}



 
Inder Kumar Rathore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the latest code I removed the event handling.....in UI there are three buttons and when i click on them nothing happen...the buttons are still present...

and in former code where I have used event handling everything is working fine........


 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Frame that contains them is referencing them. When you call add(), the Frame adds the Button to a collection; that collection keeps the Button from being garbage collected. The only reason for using the variable "b" in the original code is so that the event listener can be added to the button.
 
Inder Kumar Rathore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This means that I only have to take care of the Frame, If I put the null value in the frame object
i.e.

f=null;


at the closing of main method, the again it is visible and every thing works fine why???
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the event queue (and perhaps other internal parts of the GUI) has a reference to the Frame. To null out all those references you have to call dispose() on the Frame; then it can be garbage-collected.
 
Inder Kumar Rathore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your reply
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you may be missing here is that you're creating components for use by the AWT. When you call setVisible on the frame, the AWT event thread takes over and manages the application.

Have you wondered what happens to the application when the main() method is finished? Perhaps you haven't noticed, but that method finishes and the original thread that started it is done. The application then run via the AWT event thread.

I mention this because of your comment that one of your 'b' variables was still set. In actuality, after main() ends, all the local variables are out of scope (both 'b' and 'f').

By the way, for a simple test application, it's okay to create graphical components in the main application thread. However, you should get in the habit of creating all graphical components in the AWT event thread, using EventQueue.invokeLater(...); Here is an example:
 
Inder Kumar Rathore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Who is the parent thread of Awt event thread, JVM or main method.....
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unknown. A little investigation has shown me that the EDT is member of the main thread group, of which the main thread is also a member. That doesn't necessarily mean that the main thread is also the parent thread - there are a few other member threads. But does it really matter?
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic