Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main method

 
Tom Lee
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic