Static in the Main function
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   prashanth_vaidyaraj
Posted On:   Thursday, April 22, 2004 01:17 AM

Why should the Main function be static ? what happens if it is not declared as static?

Re: Static in the Main function

Posted By:   gaurav_saini  
Posted On:   Thursday, April 22, 2004 03:15 AM

we know very well that static function can be called from the class as well as the object scope. But in case of main() which is called by JVM it has to be static bcoz JVM calls it from the class scope.

e.g. suppose u created a class Test.java and compiled it to generate Test.class

when u execute the code u give the following command:

C:> java Test

So here we have to specify the name of CLASS not the filename. If u write something like

C:>java test

then it will give u an exception viz. ClassNotFoundException

Re: Static in the Main function

Posted By:   Simon_Ablett  
Posted On:   Thursday, April 22, 2004 02:10 AM

It has to be class rather than instance scope since you will not have an instance of the class when you run the program. You can create a non-static method called 'main' but you couldn't run the class as a program.

Regards.
About | Sitemap | Contact