Wednesday, 7 September 2016

Why static?

1.       Q) What is the requirements to declare main() method as “static” ? 
Ans):-
JVM is a software in JAVA, it was provided such a way what to access main() method by using class name which we provided on command prompt along with “java” command.
As per the predefined implementation of JVM, we must declare main() method as “static”, why because, in java applications only static method are eligible to access with class name.
Note:-
In java applications, if we declare main() method without “static” then compiler will not rise any error, because, compiler will treat the provided main() method as normal method, but JVM will provide the following.
JAVA6: java.lang.NoSuchMethodError:main()
JAVA7: ERROR: main method is not static in class Test, please defined the main method as:

public static void main(String[] args)

No comments:

Post a Comment