• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

valid syntax

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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){}
 
Ranch Hand
Posts: 119
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic