Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Regrding Access Specifier used of main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regrding Access Specifier used of main method" Watch "Regrding Access Specifier used of main method" New topic

Regrding Access Specifier used of main method

AjayP Singh

Joined: Jan 25, 2006
Posts: 1
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[])
protected static void main(String args[])

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


Ajay Pratap Singh
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 31, 2004
Posts: 11343

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.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: Regrding Access Specifier used of main method
It's not a secret anymore!