• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is "long" valid for main() ??

 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I tried running the program, though it compiled fine, it threw an error at Runtime.
But my question is that why did the compiler allow the program to compile successfully.
Is "long" valid in main??
Sonir
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the main method is just an other method. It's perfectly valid to declare the main method as you did but the problem arises when you try to run the application because the interpreter is lokking for a method called main and declared as follows:
public static void main(String args[])
replace long with void and it should run fine
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a valid method declaration. As far as the compiler is concerned, it is no different than writing
public static long aMethod(String[] stringArray)

However, if you want the virtual machine to "run" an application, must pass it a class that has a main method with this *exact* signature:
public static void main(String[] args)
Otherwise, it won't run. But as you can see, it compiles just fine.

Rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic