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?

+10 votes
asked Sep 11, 2017 in CSC 211 (F17) by Jacob Gholson (100 points)

2 Answers

+2 votes
Best answer

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, 2017 by Forrest Stonedahl (100 points)
selected Oct 31, 2017 by Jacob Gholson
+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, 2017 by Kathryn Clark (100 points)