aspose file tools*
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
Author

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.
Thx
J
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 ]

Mani
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.
--BW
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
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 my Java Programming Cookbook, see HerbSchildt.com
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,
J
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: public static void main (string [] args)