• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

valid syntax

 
laxmidhar prad
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir
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
Posts: 105
IntelliJ IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why?
  • 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
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic