Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

valid declarations of main( )  RSS feed

 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Is it legal to declare main() as follows?

public static int main(String args []) {
System.out.println("test");
return 3;
}

It runs ok when I use Eclipse, but in an online language fundamentals test at JavaPrepare.com (http://www.javaprepare.com/quests/funda_q.html Q20) it says that it shouldn't work.

Clarification appreciated,
Arthur
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will work fine, just won't be the main method the JRE looks for when starting a program. It will be just another method that happens to be named main.

I believe this to be correct, but recently have had some bad luck in answering so maybe someone else can confirm it.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can talk about it or you can do it. Doing it often gets better answers.
Write the code, compile it and see what happens.
Be sure to come back and tell us what happened.
 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, Norm.

As I mentioned in my initial post. The code runs successfully in Eclipse.

So is it fair to say that the test answer at JavaPrepare is incorrect?

Arthur
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe eclipse uses it's own implementation of the JRE, so to be sure of whether Eclipse has incorrectly implemented their JRE or whether JavaPrepare is wrong you will need to consult the JLSto see if that specifies whether the return type of main has to be void.

Section 12.1.4 will show you that Eclipse is wrong.
[ September 21, 2005: Message edited by: Joanne Neal ]
 
Julien Grenier
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I don't know which version of Eclipse you are using
but mine (3.1) doesn't run the following code


Ok Eclipse does't show any compilation error but it's normal since there's no compilation error their. but You get the following runtime error :

java.lang.NoClassDefFoundError: MainTest exception in thread "main" with the JRE 1.3.09
and with the JDK1.5 you get the following runtime error :
java.lang.NoSuchMethodError: main
Exception in thread "main"


Julien
 
Jean-Sebastien Abella
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the question was if it was legal main function... I say no, there is only one legal way of declaring the main...
and it returns void not an int.

Jean-Sebastien
 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mea culpa!

I made a mistake with Eclipse. The following method:

public static int main(String args [] ) {
System.out.println("test the main");
return 2;
}

Does *not* work, and is in fact an invalid declaration of the main() method.

So JavaPrepare was correct.

Regards,
Arthur
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hentay was correct. main is a valid identifer in Java.

public static void main();
public static void main( String[] foo);
public static void main( String[] foo, int bar);
public int main();
private final String main( int[] flubber);

All the above are valid method definitions in Java and can be used at will in a program. However, if you want a method that the JRE will attempt to execute when the program is run from the command line, only the second declaration above will do the trick.

The JRE looks for a method named 'main' which is passed a string array argument and has a void return type. Note the parameter can be named anything you want to name it - it just has to be a string array.

If you want, you can name all the methods in your program 'main' as long as they all have different method signatures. The JRE will attempt to invoke the one with the signature described above when the program is launched.

Cheers,
[ September 21, 2005: Message edited by: Tom Blough ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!