Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
    Bookmark Topic Watch Topic
  • New Topic

confusion

 
Inder Kumar Rathore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Report post to moderator

here is a piece of code

view plaincopy to clipboardprint?

1. import java.awt.*;
2. import java.awt.event.*;
3.
4. public class Activator {
5. public static void main(String[] args) {
6. Button b;
7. ActionListener al = new MyActionListener();
8. Frame f = new Frame("Hello Java");
9. f.add(b = new Button("Hello"),BorderLayout.NORTH);
10. b.addActionListener(al);
11. f.add(b = new Button("OK"), BorderLayout.CENTER);
12. b.addActionListener(al);
13. f.add(b = new Button("Quit"),BorderLayout.SOUTH);
14. b.addActionListener(al);
15. f.pack();
16. f.show();
17. }
18. }

import java.awt.*; import java.awt.event.*; public class Activator { public static void main(String[] args) { Button b; ActionListener al = new MyActionListener(); Frame f = new Frame("Hello Java"); f.add(b = new Button("Hello"),BorderLayout.NORTH); b.addActionListener(al); f.add(b = new Button("OK"), BorderLayout.CENTER); b.addActionListener(al); f.add(b = new Button("Quit"),BorderLayout.SOUTH); b.addActionListener(al); f.pack(); f.show(); } }


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......
 
Jan Cumps
Bartender
Posts: 2588
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Report post to moderator
Closing this cross-post.
Please keep discussion in your original post.
 
    Bookmark Topic Watch Topic
  • New Topic