Footprint of a class
Hi guys,
Which of the codes below has a smaller footprint?
publicclass XYZextends JFrame{
private JLabel label1;
private JLabel label2;
private JPanel panel;
public XYZ(){
init();
}
private init(){
label1 =new JLabel("label1");
label2 =new JLabel("label2");
panel =new JPanel();
panel.add(label1);
panel.add(label2);
getContentPane().add(panel);
}
}
or
publicclass XYZextends JFrame{
public XYZ(){
init();
}
private init(){
JPanel panel =new JPanel();
JLabel label =new JLabel("label1");
panel.add(label);
label =new JLabel("label2");
panel.add(label);
getContentPane().add(panel);
}
}
Is there any difference btw the two codes above in terms of footprint?
[1983 byte] By [
milyarus] at [2007-9-30 11:39:29]

This is not really the best forum for the answer you're seeking (http://forum.java.sun.com/forum.jsp?forum=32) would be much better. However it does raise an interestig issue in OO design which is on topic so I'll focus on that aspect.
In the first version your class XYZ has label1, label2 and panel as attibutes, therefore each time you create an instance of that class it will include space for the references to those members. If you only have one instance of that class you will have only one copy of those references. If multiple instances of that class exist there will be multiple instances of those variables all stored on the the heap.
In the second example I'll ignore the fact you are resusing label. The class doesn't contain those attributes, therefore the class has a smaller foot print, but that is not the whole story. Those Jlabel and JPanel classes must be stored somewhere. The classes themselves will continue to be stored on the heap, however the references to those instances will be stored on the stack. The number times those references will be stored is a function of the programs flow.
The total footprint is essential the same in the examples given, it is just a slightly different balance between heap and stack, however under different circumstances vary significantly.
Hi,
> In terms of OO design which code fits better?
It depends. In general, you should only store a reference to an object if you actually need it, so if class XYZ does not itself need to refer to label1, label2, etc. then the 2nd version is preferred, otherwise the first version.
> And is it better to use the heap or the stack up when creating objects?
It's best not to worry about it at all. Only create/refer-to objects when you need to. Let the VM handle it.
> Lastly, under what circumstances do the footprints of the classes above differ significantly?
In this particular case the footprint of Swing itself will render any differences irrelevent.
Ol.
PS. Hi Martin, long time no see...
0s at 2007-7-4 12:53:13 >

> PS. Hi Martin, long time no see...
Hi Ollie,
Yep, don't know if you heard I got layed off from KIT and I'm working at Damart in Bingley doing more development processes, UML and Patterns than Java.
I spend a most of my free net-time at the Portland Pattern Repository http://www.c2.com/ppr/ It's inhabited by some famous Patterns experts like Martin Fowler, BruceAnderson, KentBeck, JimCoplien, et.al.
I'm an apprentice there not a master like here but it is a very friendly Wiki. check it out.
p.s. I dont have your current email address, the email address in my profile here is current, so drop me a line.
Martin.