Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Is it possible to call the main method like any other method? How would you do it?

+9 votes
asked Sep 11 in CSC 211 (F17) by Jacob Gholson (514 points)

2 Answers

+2 votes
I believe that you can't really call the main method in other methods. The main method is what the computer automatically runs. The computer starts in the main method and uses the other methods if they are called within the main method. You can't call a method within its own body.
answered Sep 11 by Kathryn Clark (1,391 points)
+2 votes

Technically, yes.  However, it doesn't make very much sense for you to do this (at least at this point).  It also requires knowing something about "arrays", which don't come up until chapter 7 of our text.

But... since you asked, you would call main by writing something like this:

String[] arguments = {"apples", "bananas", "cherries"};

However, as Kathryn points out, if a method calls itself every time it runs this would cause a weird recursive loop, which will eventually cause an error.  However, you could also have one class's main method call another class's main method...

answered Sep 11 by Forrest Stonedahl (3,438 points)