Let’s say I have this custom component. It subclasses JMenuItem and all instances use the same Font object, although none share the same instance. For example,
public abstract class JFooMenuItem extends JMenuItem{
public JFooMenuItem(final String title){
super(title);
setFont(new Font("Courier New", Font.BOLD, 12));
}
}
Now, given that there may be up to 10+ menu items, would it be more efficient to make the Font instance a shared, static member variable, or is this current setup (i.e. the code above) just fine (memory-management-wise)?
I would say use one named instance, not because of the memory, but because if you decide to change the font, you have to edit at
10+places.public static final Font MENU_FONT = new Font("Courier New", Font.BOLD, 12);Edit: Even if you use subclassing, better declare it as
public static finalbecause theFontis constant. It is more clear.