I've seen objects created in Java code without storing a reference to the object. For example, in an eclipse plugin I've seen a SWT Shell created like so:
new Shell();
This new Shell object is not stored in a variable, but will remain referenced until the window is disposed which [I believe?] happens by default when the window is closed.
Is it bad practice to create objects like this without storing a reference to them? Or was the library poorly designed? What if I don't have need of a reference, but only want the "side effects" of the object? Should I store a reference anyways?
UPDATE:
Admitedly, my above example is poor. While I have seen UI elements created like this, creating a SWT Shell like this would probably be pointless because you need to call the open method on the Shell instance. There are better examples provided by aix such as the following from the Java concurrency tutorial:
(new HelloThread()).start();
This practice is seen in many contexts, so the questions remains. Is it good practice?
Shell.createTopLevelShell()
or whatever, vs using a constructor in this case. But functionally there's little difference.) – Daniel R Hicks Jan 12 '12 at 20:25dispose()
d: Rule 1: If you created it, you dispose it. http://www.eclipse.org/articles/swt-design-2/swt-design-2.html – jbindel Jan 12 '12 at 20:40