• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

From a OO Point of View in GUI design

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need help. From a OO point of view in designing a GUI, what is the difference between the following code. Precisely, what is the significance of putting all those swing components as the the class private variables compared to defining them as variables only local to a method.
public class test {
private JFrame frame;
private JPanel panel;
private JButton button = new JButton("hello");
public displayFrame()
{
frame = new JFrame();
Container content = frame.getContentPane();
panel = new JPanel();
panel.add(button);
content.add(panel);
frame.visible(true);
}
AND
public class test {
public displayFrame()
{
JFrame frame = new JFrame();
Container content = frame.getContentPane();
JPanel panel = new JPanel();
JButton button = new JButton("hello");
panel.add(button);
content.add(panel);
frame.visible(true);
}

 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you declare the type and identifier locally, you'll lose those references at the completion of the method. The AWTThread will maintain its own references to them, so there's no fear of garbage collection if you write it that way.
Should you want to manipulate one of those objects from some other method, you'd have to find some other way to get a reference to it first.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic