File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "run time error" Watch "run time error" New topic

run time error

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".

Stephen Pride
Ranch Hand

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

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.
I agree. Here's the link:
subject: run time error
It's not a secret anymore!