Cool discovery of the day: At least as of JDK 1.3, you do
not have to declare your main()
methods
public
!
You can leave them with the default package access, or even make them private!
This was reported to me by a Java trainer in India, one of whose students
(as yet anonymous) noticed this.
This has a lot of important implications; among them:
main()
methods that are only used for testing
and not for application launching, can be private so they aren't
accidentally accessed from other classes, and don't need to be
deleted or commented out before distributing or
building JavaDocs.
I still recommend that you make the main()
methods that launch your applications public. This allows the program to
be easily launched from or as a part of other Java programs.
It makes Java a lot more scriptable from within Java.
Nonetheless non-public main()
methods are
a useful and potentially important technique.
This does not work in Java 1.1.3 (the only other version I have easily available.) Can somebody test this in Java 1.2? Here's a simple program to try:
class Test {
static void main(String[] args) {
System.out.println("Hello World!");
}
}
Currently, this program compiles everywhere I've tried it. It runs successfully JDK 1.3.0 for Windows. It fails with the message "In class Test: main must be public and static" on JDK 1.1.3 for Solaris. I suspect this is an unannounced or perhaps announced but little heralded change in either Java 1.2 or Java 1.3 that's just being noticed now.
Several people have reported that the non-public main()
method works in JDK 1.2.1 and JDK 1.2.2.
It does not work in JDK 1.1.8, JDK 1.1.5, or JDK 1.0.2.
However, Jamie Lawrence reported that it failed in Borland JBuilder regardless of JDK version.
Robert A. DiBlasi found that this is addressed as
Bug Report
4252539 in the Java Developer Connection Bug Database.
Apparently the behavior is intended, but implementation dependent.
Scott Came pointed out the first edition of The Java Language Specification--that is, the
one that covers Java 1.0--is unequivocal
on the issue. Section 12.1.4 states that
"The method main
must be declared public
, static
, and void
."
My draft version of the second edition of the spec retains this exact sentence in section 12.1.4.
Would any language lawyer with a copy of the final version 2 of the Java Language Specification
care to point out if that spec still includes this sentence?
(If XMLDevCon hits a slow spot today,
I may pop over to the McGraw-Hill bookstore and buy one myself.)
The final version of the second edition of The Java Language Specification (JLS)--that is, the
one that covers Java 2--includes the same language in
Section 12.1.4:
"The method main
must be declared public
, static
, and void
."
Given this statement, I don't think I'll be recommending or using non-public main()
methods just yet.
However it still leaves open a couple of questions:
main()
method? or for that
matter a non-static or non-void main()
method? I suspect it's not, but the issue is unclear from the spec.
main()
method?
I suspect the answer is yes. However, the JLS isn't detailed enough to definitively answer either of these questions. Normally, these are the sorts of issues that would be resolved by a standards committee. Too bad Java doesn't have one.
If you'd like to add your own comments on this topic, write them up and email them to elharo@metalab.unc.edu. I reserve the right to edit for style and content as well as to select which reports are and are not posted. Generally speaking, the most comprehensive, accurate, and interesting reports will be posted; especially those that introduce new information not already included here. Please be sure to tell me that what you send is intended for publication. By default, I assume that personal email is a private communication and will not republish it unless told otherwise. At the same time, if you specifically wish to remain anonymous, tell me that too. Otherwise I'll assume it's permissible to cite you as the source of a particular piece of information or story. Thanks!