• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

valid syntax

 
laxmidhar prad
Ranch Hand
Posts: 33
  • 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: 119
Hibernate Spring Java
  • 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
Marshal
Posts: 64690
225
  • 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
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!