• 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

Signature of main()

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:
I got this question from one of the Certify book:
Which of the following signatures are valid of the main() method entry point of an application?
A. public static void main()
B. public static void main(String arg[])
C. public void main(String [] arg)
D. public static void main(String []args)
E. public static int main(String [] arg)
The answer is B, D, and E.
Could anyone explain me why E? Is it the same kind of thing as in C language?
Thanks,
Kabong
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I beleive that the most important about main() is that:
1. it be public (so it can be called from outside)
2. it be static (so it can be instantiated by its class name)
3. it accepts an array of String as parameters. They array does not have to be specific, you could use the following and it would still be ok.
public static void main (String [] yahoo)
The return type is not that important, because you don't return anything from main anyways.
Hope this helps
Salut
Mario
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I think that's wrong. From the JLS, §12.1.4 Invoke Test.main:


The method main must be declared public, static, and void. It must accept a single argument that is an array of strings.


If you don't believe the JLS, try compiling and running this:

You'll find that you get a NoSuchMethodError in return.
Corey
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, note the placement of the array brackets is pretty flexible in java.
String[] args
String [ ] args
String []args
String args[]
String args [ ]
are all valid.
For a two-dimensional array you can even do this, although it's very ugly and bad style:
String [ ]twoDArray[];
The preferred style is:
String[][] twoDArray;
 
Mario Levesque
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey,
You are right. The main method has to be void.
The syntax is ok to compile without errors. The error comes out at runtime.
Mario
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because at runtime, the JVM is attempting to invoke a method with the signature static void main(String[]). If it can't find it, you'll get a runtime error:
java.lang.NoSuchMethodError: main
Exception in thread "main"
You won't get a compiler error because if you create a method with this signature:
static int main(String[])
Then this is just another, non-special method as far as the compiler is concerned, and since there is no syntax error, it compiles fine.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed. My guess (although Ive looked through the JLS and JVMS and haven't been able to find it spelled out) is that, in order to invoke the main method of your class, the JVM uses some sort of reflection to find and invoke the method. If you've ever used reflection, you know that the return type is very important as "int main" and "void main" are very different methods.
Perhaps someone else knows for sure if this is the case.
Of course, you're always allowed to have other signatures for the main method. The following is perfectly legal:

Now, we're just into a simple case of overloading a method - in this case, we're just overloading a somewhat special method.
Just remember, the one that the JVM will invoke is the main method that is public, static, returns a void, and takes a single String array as an argument.
Corey
 
Mario Levesque
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alrighty then,
So the answer set from that book is wrong and E shouldn't be part of the answer.
Do we all agree?
Mario
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't really think there was much debate but, to wrap up this thread, the correct answer for the question is:
B & D
A, C, and E are invalid declaration of main as an entry point.
A is incorrect because it doesn't take a String array as an argument.
C is incorrect because it isn't static.
E is incorrect because it returns int, rather than void.
Ta Da!
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, I agree.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about a main method with private or protected accessibility? On jdk 1.3.1, the application will run fine.
I am a bit confuse.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the method main does not have the expected signature which is
public static void main(String[] arg)
and the compiler doesn't object, and the program run fines, then the JDK you are using does not comply with the specification (JLS) and your program may not run with another JDK version. If you code like that, you do it at your own risk.
 
Eric Low
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't be that all JDKs fully comply with the JLS?
I am preparing for the exam next week and one of the best way I believe is to do coding a lot. It will be confusing if the JDK is not fully comply with the JLS.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't be that all JDKs fully comply with the JLS?
In an ideal world yes
one of the best way I believe is to do coding a lot.
You got it
It will be confusing if the JDK is not fully comply with the JLS.
When in doubt, the JLS is the last authority!
 
Eric Low
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Thanks a lot.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I just found out that "public static final void main(String [] args)" is also correct and it compiles.
Kareem
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case you won't be able to hide the main method in your subclasses. Say for instance, you have a small test unit in each of your class that you can launch via the main method, then if you declare main final, you won't be able to declare new main methods in your subclasses.
just my $.2
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic