Just wondering if the following is considered to be good programming practice or not? I like to keep my individual source files as concise and uncluttered as possible, but I’m wondering what more experienced coders would think of it. I especially like the idea of the Settings.java class to keep all of my “Magic Numbers” in the one place. Has anyone any suggestions as to how I might improve on things?
Happy coding 🙂
class ApplicationLauncher
{
public static void main(String[] args)
{
SwingApplication mySwingApplication = new SwingApplication();
}
}
//////////////
import javax.swing.*;
public class SwingApplication extends JFrame
{
public SwingApplication()
{
JFrame myJFrame = new JFrame();
myJFrame.setSize(Settings.frameWidth, Settings.frameHeight);
myJFrame.setVisible(true);
}
}
//////////////
class Settings
{
static int frameWidth = 100;
static int frameHeight = 200;
}
There’s nothing wrong with having a settings class; however, in your example the settings are rather ambigious in terms of what frame they apply to, neither are they actual settings, but rather default values that strictly belong in the SwingApplication class.
Another thing which we don’t have much control over is how the constructor call in Swing cascades into the program’s message pump loop.
To me it has never made sense with a constructor that never returns (unless the frame is closed) and does more than initialize an object.