This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regrding Access Specifier used of main method

 
AjayP Singh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know what is the significance of public access modifier with main (String args[]) method. Like generally we write
public static void main(String args[])
{
}

But if we write
private static void main(String args[])
{
}
OR
protected static void main(String args[])
{
}

then also its working properly.............
then what is the use of public keyword..........................

Regards

Ajay Pratap Singh
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works properly when compiling.

But have you tried running the program that doesn't have the keyword public in the header of the main method?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As of Java 1.4, main methods are required to be public in order to be program entry points (i.e., run from a command prompt).

A non-public method called "main" is perfectly valid -- it just can't be used as a program entry point.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic