File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Accessibility of main() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessibility of main()" Watch "Accessibility of main()" New topic

Accessibility of main()

Greg Torrance

Joined: Feb 25, 2000
Posts: 17
Hi there,
I am new to the forum - (great group BTW!) - and was wondering if someone can clear something up for me. I keep reading that the main() method must be defined as public and static, but when I run a test with a private or protected main() method, it works. Why is this?
For example, if I create class Test1 as follows:
class Test1
private static void main( String[] args )
System.out.println( "private main" );
And then I run it as
java Test1
I get the "private main" message.
Any thoughts?
Thandapani Saravanan
Ranch Hand

Joined: Oct 17, 1999
Posts: 117
Actually this has already been discussed a couple of times. As per JLS the method must have the following signature:
public static void main(String args[])
If the compiler allows other versions, then we could say it is a bug in compiler. At the same time, we don't get anything more by changing the specification, do we?

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249


Is that really a bug or there is something we are really missing...

faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
yep !! it was a bug, in older java versions that is prior to java 1.4 , we can make main method as default or private.

Though it has been rectified now.

I agree. Here's the link:
subject: Accessibility of main()
It's not a secret anymore!