Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "main method" Watch "main method" New topic
Author

main method

Tom Lee
Greenhorn

Joined: Aug 25, 2005
Posts: 15
Hi all,
I am new to Java Tech., my query is, why we can not write main method as private or protected.


Regards,
Tom
Christian Spreiter
Greenhorn

Joined: Nov 07, 2005
Posts: 5
I'm also pretty new to java, so it may not be 100% correct.

why would you want to do that? there is no point in doing so because it's the first method that get's called and starts your programm.
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Originally posted by Tom Lee:
Hi all,
I am new to Java Tech., my query is, why we can not write main method as private or protected.


Regards,
Tom


You can write but when you run the program using java as it can not access main method the class do not run.
Vinayak patil
Ranch Hand

Joined: Aug 30, 2005
Posts: 67
Hi,
This is my understanding:

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.


"I can resist everything except temptation"
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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.

See, I told you this was pedantic
Gaurav Chhabras
Ranch Hand

Joined: Sep 21, 2005
Posts: 126
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.
 
Consider Paul's rocket mass heater.
 
subject: main method