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

5 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

Naviya Nair said...
24 October 2016 at 15:16

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java 8 Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai Java Training Institutes

Java Training Institutes in Chennai Java Training in Chennai

Post a Comment

Popular Posts

Subscribe via Email
Subscribe Java Interview Questions via Email
All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar
blog links Web Directory Academics Blogs