JDK-6349917 : add support for non-static "main" method
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows
  • CPU: x86
  • Submitted: 2005-11-14
  • Updated: 2010-05-09
  • Resolved: 2008-09-02
Related Reports
Relates :  
Description
This is a small Ease-of-Development request for Dolphin.

By convention, a java program is started by calling a static "main"
method on a target class.

However, it is inconvenient for the "main" method to only be able to
access static methods and instances in the main class.  So it is
common for people to do things like:

    public class Main {
    
        public void doit(String argv[]) {
            // real code goes here.
        }
        
        public static void main(String argv[]) {
            (new Main()).doit(argv);
        }
    }
    
I would like us to also accept a new pattern.  If a class
provides a zero arg constructor (either the default constructor
or a human written zero-arg constructor) and it has an instance
method "public void main(String argv[])" then we should be willing
to treat that main method as the entry method for the program.

This can be implemented by having the runtimes allocate a new instance
of the target class and then call the main instance method.

If a class has both a static "main" and an instance "main" then
we should call the static "main", for compatibility with existing apps.

I believe making this small change will be a welcome small EoD
improvement for many developers!

Thanks    - Graham

Comments
EVALUATION JVMS 5.2 specifies that 'main' is static: "The Java virtual machine then links the initial class, initializes it, and invokes its public ***class*** method void main(String[])." JLS 12.1 essentially defers to the JVMS on the topic, so I do not interpret the JLS as permitting a non-static main. My initial view on the proposal was: - I concede that the pattern in the Description is fairly common. - I concede that 'static' is hard to explain to beginners, and that: public class Main { public void main(String argv[]) { System.out.println("My first object"); } } is easier to explain simply. - It is tinkering, but with the aim of lowering barriers to entry, and that is important. However, people are not leaving Java because main is static, and they are not going to refuse to learn Java because main is static. I'm aware of the "Essence v. Ceremony" argument (Java has too much ceremony/verbosity that obscures the essence/intent of your code) but this RFE is change for change's sake. Specific disadvantages of this proposal: - It's too easy to make a class that cannot be launched. Consider adding the one-arg constructor below but forgetting a no-arg constructor: public class Main { // Main(int i) {} public void main(String[] a) {} } - A class with just a non-static main can only run on new launchers (why do we want to fork the language like this?) and the JVMS would have to allow static/non-static main depending on classfile version (yuk). - A classfile cannot have two methods called main with the same descriptor 'main(String[])'. It doesn't matter that one is static and one isn't. Special-casing the JVMS to allow duplicate mains is ridiculous, and will break reflective programs that assume they'll find a single, static, main.
02-09-2008

EVALUATION This new policy would require changes to the launcher.
14-11-2005