Back to Top

Sunday 15 January 2012

JAVA: What if the main method is declared as private?

The program compiles properly but at run-time it will give "Main method not public." message


4 Responses to “ JAVA: What if the main method is declared as private? ”

suri said...
6 June 2012 at 07:51

what happens main method write like this.... public void main()


bharath said...
11 June 2012 at 18:15

it will compile but we get the error while executing the code with an error as "NoSuchMethodError".because the JVM calls the main() by the class name with static keyword.i.e.,

Note:With static keyword we can call the any method by using class name itself,no need of any object creation and calling the respective method.

In the same way the JVM calls the main() with class name by using the static keyword.So,static is mandatory to write.


sudhanshu said...
14 September 2012 at 23:47

No compile error.
But, main method will not be accessible by JVM.


Anonymous said...
26 June 2013 at 04:00

Run time error


Post a Comment

Popular Posts

Subscribe via Email
Subscribe Java Interview Questions via Email
All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar