Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes public static void main (string [] args) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public static void main (string [] args)" Watch "public static void main (string [] args)" New topic

public static void main (string [] args)

Jay Brass
Ranch Hand

Joined: Oct 24, 2000
Posts: 76
Hey all,
I've been doing Java for a few years and recently had a question from an interviewer ask what the correct signature of the main method was. I obviously chose (multiple choice answers) the signature:
public static void main (String [] args)
but I remember reading somewhere where the format wasn't important. Meaning it could be:
static public void main(String [] args) or
void public static main (String [] args) or
public static void main (String messages []) or even
public void static main () <- with no arguments
Is this true? or was I not reading about methods? In case you are wondering I circled all of the above and shame on me for not knowing such a simple question.
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
The order is important to an extent.
You can swap static & public (i.e. both "public static void main" and "static public void main" are fine), but you cannot specify the return value at the beginning. So "void static public main" will not even compile.
Regarding the no arguments main() method, it will compile, but you can't execute the class. It will throw a NoSuchMethodError, because the JVM expects the main() method to have a string array as the parameter.
[ December 02, 2003: Message edited by: Mani Ram ]

Quaerendo Invenietis
Brian R. Wainwright
Ranch Hand

Joined: Aug 12, 2003
Posts: 92
This is true to an extent. You can call the String array anything you want and declare it as String[] args or String cheeseSteak[]. You're also allowed to switch the public and static modifiers around.
Herb Schildt
Ranch Hand

Joined: Oct 01, 2003
Posts: 253
Both Mani's and Biran's posts above are right. However, the traditional, and commonly used, format is:

I think that it is best to stay with this format.
[ December 02, 2003: Message edited by: Herb Schildt ]

For my latest books on Java, including Introducing JavaFX 8 Programming, see
Jay Brass
Ranch Hand

Joined: Oct 24, 2000
Posts: 76
Thanks guys,
I think my major problem is that the IDE's write so much syntatically correct code that I never gave that a second thought. I actually had a brain fart when I read the question. I should have stayed with my initial thought but I'm a victim of over evaluating.
and since you all had your little sayings let me add one to the stack..
Its currently a problem of access to gigabitis through puny baud.....
That is what they call a brain fart!
Thanks again,
I agree. Here's the link:
subject: public static void main (string [] args)
It's not a secret anymore!