Calling the main method within main

I've got a public main method that I want to restart depending on user input. In other words I want to call "main" from within main, is this possible? Howto?

I've done this to loop instances of functions in JavaScript, but how is this done with java?

Example:

public class Anyname{

public static void main (String[] arg) {

...

if (JOptionPane.showConfirmDialog(null, "Again?", "Q:", JOptionPane.YES_NO_OPTION)==0) {

call main; // or WHAT?

} else {

System.exit(0);

}

}

}

[551 byte] By [hagdahla] at [2007-10-3 3:18:46]
# 1
It is just a static method so you could say:main(null); or main((String[])null); or main(myStrings); or etc.However, you would be recursively calling the function just to perform a loop and in this case a BadThing(TM).Put your code in a "while" statement instead.
jbisha at 2007-7-14 21:10:35 > top of Java-index,Java Essentials,New To Java...
# 2

You can do this, but ...

public class RedoMain {

static int i;

public static void main ( String[] argv ) throws Exception {

String[] s = null;

System.out.println("["+(++i)+"]");

if (argv[0].equals("Y"))

RedoMain.main((s = new String[] {"N"}));

}

}

abillconsla at 2007-7-14 21:10:35 > top of Java-index,Java Essentials,New To Java...
# 3
I just had a Lisp flashback...(you just have to squint hard enough that the curly braces look like parentheses)Message was edited by: tsith
tsitha at 2007-7-14 21:10:35 > top of Java-index,Java Essentials,New To Java...
# 4
Thanks for the answers. Problem solved. I'm a happy rookie!
hagdahla at 2007-7-14 21:10:35 > top of Java-index,Java Essentials,New To Java...
# 5

first if the user types yes more time than the size of your stack, you will get a stackoverflow error

second, this is really bad programming, you need do while there

public class RedoMain {

static int i;

public static void main ( String[] argv ) throws Exception {

do{

// do main stuff here

}

while(JOptionPane.showConfirmDialog(null, "Again?", "Q:", JOptionPane.YES_NO_OPTION)==0)

}

}

dont forget to assign your duke dollars to people when your question is answered

JF_Beaulaca at 2007-7-14 21:10:35 > top of Java-index,Java Essentials,New To Java...