This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public static void main (string [] args)

 
Jay Brass
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Brian R. Wainwright
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jay Brass
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic