When the JVM has to execute your program, it looks for a main() method with teh following signature public static void main(String args) . If the method is private or protected, it cannot call this method. Hence, the main() method has to be public.
Just a historical footnote: some earlier versions of the JVM couldn't enforce the public restriction, so one could have gotten away with writing the main method with any level of access -- not that this was a good thing.
A small tip: the allowed signatures of main permit it to throw exceptions, so this is fine:
This is handy when whipping of a quick example, versus having to write
There is no emoticon for what I am feeling!
Joined: Sep 16, 2005
Originally posted by Tom Lee: I am new to Java Tech., my query is ...
This is pedantic, but why not just write "I am new to Java, ..."? What does the word "technology" add to the question? So many people do this and it just reminds me of the time when garbage men started to be called "sanitation engineers". I know there are times when one wants to make the distinction between just the language Java and all the APIs and tools that gets dragged in under the Java technology rubric, but let's not over do it.
The basic reason is that public allow you to access it from anywhere , so when you compile the programme it will not give any error but when you run means at the runtime it will give an error, because you are not allows to reduce the access specifier of the function main.