• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

HELP

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a question comes up in the exam which asks
Whic of the following are correct???
a) public static void main(String args[])
b) private static void main(String args[])
c) protected static void main(String args[])
d) public static void Main(String args[])
I know that a, b and c are correct and the compile and rund without any errors.
But from the exam point of view should my answer be only a or all of a, b and c???
Takin the exam tomorrow.....
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the purposes of the exam, the only correct answer is:
public static void main(String[] args)
This is what the JLS says and we have to go with that.
All the best for your exam
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry guys, but what does you mean by saying "For the purposes of the exam"?
The question is "Which of the following are correct?". So, my understanding is what are the correct methods declarations?
So, all the four answers are correct.
public static void Main(String args[]) is completely correct declaration.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yanish,
That declaration is incorrect because Main begins with an uppercase M not lowercase m. Otherwise, it would be correct.
BTW will the exam really try to test these other strange method declarations (i.e. final, private, protected)?
Lee.
 
yanish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lee,
I agree with you that it's not a good way of coding, but nothing forbids you to have a method with a capitalized name.
compiles and runs perfect.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yanish,
I agree with you that it compiles fine. But it will not run fine unless you have an other main method with a lowercase 'm'. If you attempt running a class with the main method described by you, you will get NoSuchMethodError.
The main method which is the starting point of execution of a class file has the signature public static void main(String args[]) . If you attempt to run a program using java.exe, and if the JVM cannot find the main method with this signature , you will get java.lang.NoSuchMethodError:main.
Since Java is case sensitive, any other version of the main method can exist in a class. Note that main(), Main(), mAin(), mAIn() are all different methods. The important thing to remember here is, if you want your class to be executed by JVM, you better have a required main method.
If you have ever experimented witht the main method, the public modifer is actually not required for a program to run. ie., main methods with private or default modifiers works as well. However, for the sake of the exam, there is only one acceptable signature of the main method.
Hope this helps,
Ajith
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes you just can't be sure what the answer would be:
Is the following declaration of a method legal?
public void (int k, final double d)
True or false?
I would choose false because the following is more appropriate
public void (int k, final double d){//code}
but the answer given was True (I think it is from RHE). the same logic applies to
public static void main(String []args}
Do we need the braces for it to be correct? what does correct mean anyway? correct signature or correct decalartion or correct
declaration of THE main method of a java program. I don't mean
to pick on the question but I just don't know what the best policy would be if an ambiguous question like those two comes up in the real test?
Peter
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic