TempusEdaxRerum
TempusEdaxRerum

Reputation: 31

Is it bad practice to overload your main method?

I am aware of the limitations of it, but if I only intend on having a class, say, for example

public class GUIWindow
{
    static JFrame theGUI = new JFrame();

    public static void main(String[] args)
    {
        theGUI.setSize(900, 600);
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theGUI.setLocationRelativeTo(null);
    }

    public static void main(Object obj)
    {
        String[] array = new String[1];

        main(array);

        theGUI.setTitle(obj.getClass().getName());
    }

    public static void main()
    {
        String[] array = new String[1];

        main(array);

        theGUI.setTitle(null);
    }
}

that I can call to create a default GUI window of a certain size for the testing of multiple applications is it an alright thing to do?

Upvotes: 1

Views: 323

Answers (2)

9000
9000

Reputation: 40894

public static void main(Sting[] args) is the only entry point JVM would recognize. You could add other overloads (not overrides) of main, but these will lack that special meaning. It feels inconsistent and thus misleading.

If you want polymorphic instantiation of your main class, just add an independent family of methods for this. Don't mix it up with one predefined special method.

Upvotes: 6

greedybuddha
greedybuddha

Reputation: 7507

The main method public static void main(String args[]) should be used to set up your program and verify that the incoming arguments to your program are valid. Anything else is usually considered bad practice. This includes "overloading" your main method.

That being said, if you are just doing toy examples, or testing, do whatever you want in your main methods.

Upvotes: 1

Related Questions