• 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

mainly about main( ) ....

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...
pl help me with these qns..these are from javaprepare.com
1.What would be the results of compiling and running the
following class. Select the one correct answer.
class test {
public static void main() {
System.out.println("test");
}
}

A.The program does not compile as there is no main method
defined.
B.The program compiles and runs generating an output of "test"
C.The program compiles and runs but does not generate any
output.
D.The program compiles but does not run.

I thought the compiler must complain 'wrong number of arguments in main()' because main() takes an array of String as argument..But that is not one of the options, and the answer is given D.
cud somebody explain this ?

2.Which of the following are valid declarations for the main
method. Select all correct answers.
A. public static void main(String args[]);
B. public static void main(String []args);
C. final static public void main (String args[]);
D. public static int main(String args[]);
E. public static abstract void main(String args[]);
My answers are A and B , The answers given are A,B and C.Could somebody explain this?
This seems to be quite vague to me.Cud someone explain the legal access modifiers of the main() method? And , I remember having read in an earlier post here that the modifier private in the main method will not give any error ,I am not clear how that is possible too..
Thanks in advance,
Vidya.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vidya,
The first program compiles but does not run. It compiles because the given method is valid and main is a valid method name. To run the program a method with the signature
public static void main(String[] whatever)
is required which is not defined in the class.
-Vani
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Vani got it right.
The famous public static void main{String whatever[] ) is the entry point of any Java class file. This is the necessary and sufficient condition that must be met if you want your program to run. However, if your class declares any other version of main() method whose signature differs from the one mentioned above, it is plain old overloading. It is perfectly legal and compiler has no problems with it.
Also, adding final qualifier to the standard main method signature doesnt seem to affect it. It is true that it runs quite fine with private or default modifer too. SUN has acknowledged this inconsistency but they feel it is not a bug and it is not going to get fixed. However, for the sake of SCJP, the acceptable signature must have public access modifier.
Look out for this kind of gotchas in the exam.
Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, can we select c also for SCJP???
And if any string value is to be written
is textfield, should the string be enclosed
in "".
[This message has been edited by avn (edited August 23, 2000).]
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
avn,
Yes, for the second question about the main function, you can select the option 'C'. Adding the final qualifer to main method is not illegal and does not change the behaviour of the method. Nothing precludes you from doing so.
For fill-in answers, do not enclose them in quotes, unless the question asks you to do so explicitly.
Hope this helps,
Ajith
 
vidya
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vani,avn and Ajith ..
:-)
regards,
Vidya.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic