Cafe au Lait Special Report: Non-public main() methods

Reports

Tuesday, June 27, 2000

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:

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.

Wednesday, June 28, 2000

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.)

Thursday, June 29, 2000

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:

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.

External Links and Reports


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!


Cafe au Lait Home | Java Books | Java Trade Shows | Java Course Notes | Java FAQ | Java Tutorial | Java User Groups | Java Mailing Lists | Java Questions | Java Quotes

Copyright 2000 Elliotte Rusty Harold, elharo@metalab.unc.edu
Individual reports copyright by their respective authors.
Last Modified on Saturday, July 1, 2000