calling main method from another method in the same class

How to call the main method?

void prompt()
{
    System.out.println("Do you want to continue?");
    Scanner confirm = new Scanner(System.in);
String con = confirm.nextLine();
if (con  == "y")
{
//call the main method once again.
}
}

When I use main(); It asks for the value of “args” yet I’m not sure what value I should put in it.

Answer

The main() method in a java program takes a String array argument.

public static void main(String[] args) {} 

If you do not use the variable args inside of main() you could just pass null to it. Otherwise you would need to pass a String array to the method.

However, you should not be calling the main() method from inside your application. The main() method should be used as an entry point into your application, to launch a program, not be used to recursively execute the logic inside that application. If you have functionality needed again you should put it in a separate method.

Leave a Reply

Your email address will not be published. Required fields are marked *