File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes valid syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "valid syntax" Watch "valid syntax" New topic

valid syntax

laxmidhar prad
Ranch Hand

Joined: Feb 05, 2006
Posts: 33
kindly tell me Which of the following are valid syntax for the main() method required by a Java application? & why

A. public static void main(String args[]){}
B. static public void main(String args[]){}
C. static public void main(String[] args){}
D. public static void main(String[] args){}
E. static public void main(String args){}
Wayan Saryada
Ranch Hand

Joined: Feb 05, 2004
Posts: 105

(E) is invalid, as the main method in Java should accept a single array of string as parameter, in this case (E) is just an ordinary static method named main that accept string as parameter.

Aswer (A) to (D) is a valid main method in Java. We can write String[] args or String args[] as the parameter.

Website: Learn Java by Examples
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 42693
  • Because in C they had the concept of a main file which controls the rest of the programming, and it had a method in called main whihc the compiler and runtime look to to start the program off.
  • Because Java is derived from C via C++
  • because they told the JVM to look for a method called main
  • Becuase there is nowhere for it to return a result or value to (hence "void").
  • Because access is required from outside the class, it has to be "public"
  • Because access is needed before an object is created, it has to be "static"
  • Because one can write "static public" as a synomym for "public static," but please stick to "public static."
  • Because the command line passes an array of Strings to the main method.
  • Because one can write String[] args, String args[], or (I think) String ... args as synomyms, but String[] is best because it confirms that the object passed has a String-array type.
  • And you can use another identifier instead of args (argv and arguments appear in older sources), but again, please stick to "args."
  • CR
    I agree. Here's the link:
    subject: valid syntax