• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

question on main

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 9
class GRC1 {public static void main(String[] args) {}} // 1
class GRC2 {protected static void main(String[] args) {}} // 2
class GRC3 {private static void main(String[] args) {}} // 3

What is the result of attempting to compile each of the three class declarations and invoke each main method from the command line?

a. Compile-time error at line 1.
b. Compile-time error at line 2.
c. Compile-time error at line 3.
d. An attempt to run GRC1 from the command line fails.
e. An attempt to run GRC2 from the command line fails.
f. An attempt to run GRC3 from the command line fails.


and the answer to this question is given as

e.An attempt to run GRC2 from the command line fails.
f.An attempt to run GRC3 from the command line fails.

but when i tried to compile and run this program with all the three class names. it compiled and run well with no output. please clear me which is the correct answer.


class GRC1 {public static void main(String[] args) {}} // 1
class GRC2 {protected static void main(String[] args)
{
System.out.println(args[0]);
}
} // 2
class GRC3 {private static void main(String[] args) {
System.out.println(args[0]);
}
} // 3


This code also worked well and pinted the string in the commandline argumed for each of class run
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe this answer is specific to Java 1.4 or later.

My understanding is that prior to Java 1.4, main() as an entry point was not actually required to be public. I have Java 1.3 here at work, and main() works as an entry point with any access modifier (including default).

Note that "main" is not a keyword, and you can define any sort of main method you like. However, as an entry point, main() must be static, return void, and take a String[] argument. As of 1.4, main() must also be public.

So in the above question, all of these will compile; but with Java 1.4 or later, only the public main() can be invoked from the command line.
[ September 10, 2004: Message edited by: marc weber ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic