Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

run time error

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one explain me why the following snippet compiles but gives a run time error?
class Myclass {
public static void main(){
System.out.println( "Print");
}
}
 
Stephen Pride
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the class should be "public".
 
Stephen Pride
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... plus main() should be main(String[] args).
 
SathishVJ
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java VM expects the function with signature
public static void main(String args[])
to be necessarily present when running an application. The VM calls this particular method to run your program.
When you compile your code, the VM has no idea that you are going to run the application using this class. So it compiles assuming that the method
public static void main(){...}
is simply another method like
public static void someMethod(){ ... }
But then you hit an exception when you attempt to run it coz the method required to start it is not present.
That's a long ans.
Regds
Sathish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic