aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes run time error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "run time error" Watch "run time error" New topic
Author

run time error

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Sep 14, 2000
Posts: 121
I believe the class should be "public".


SCJP
Stephen Pride
Ranch Hand

Joined: Sep 14, 2000
Posts: 121
... plus main() should be main(String[] args).
SathishVJ
Greenhorn

Joined: Sep 07, 2000
Posts: 16
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: run time error