File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic


Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41

here is a piece of code

view plaincopy to clipboardprint?

1. import java.awt.*;
2. import java.awt.event.*;
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();
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();; } }

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......


Living is easy with your eyes closed.....
Jan Cumps

Joined: Dec 20, 2006
Posts: 2565

Closing this cross-post.
Please keep discussion in your original post.

OCUP UML fundamental and ITIL foundation
I agree. Here's the link:
subject: confusion
It's not a secret anymore!