the main method in an interface

Leonid Kuskov leonid.kuskov at oracle.com
Thu Mar 12 23:43:10 UTC 2015


Hello Alex,

JVMS and JLS don't take into account that the main method can be in an 
interface.
Assertion jvms-5.5-110-F states "If C is a class, its designation as the 
initial class at Java Virtual Machine startup".
But C may be an interface also. HotSpot initializes such interface 
successfully.
$ cat Intf.jasm
public interface Intf
         version 52:0
{

public static Method main:"([Ljava/lang/String;)V"
         stack 2 locals 1
{
                 getstatic       Field 
java/lang/System.out:"Ljava/io/PrintStream;";
                 ldc     String "Hello, world!";
                 invokevirtual   Method 
java/io/PrintStream.println:"(Ljava/lang/String;)V";
                 return;

}

static Method "<clinit>":"()V"
         stack 2 locals 0
{
                 getstatic       Field 
java/lang/System.out:"Ljava/io/PrintStream;";
                 ldc     String "initialized";
                 invokevirtual   Method 
java/io/PrintStream.println:"(Ljava/lang/String;)V";
                 return;
}

} // end Class Intf

Output:
$ java Intf
initialized
Hello, world!

Looks like JLS and JVMS don't take into account interfaces with the main 
method in other places too (JVMS 5.2, JLS 12.1, maybe more).

And second question is:

Umbrella assertion jvms-5.5-110 includes assertions about the ways how a 
class can be initialized (execution of 
new/getstatic/putstatic/invokestatic, invocation of MethodHandle etc).
Each of them, except invocation of reflective methods, clarifies that a 
class will be initialized only if it hasn't been initialized already.
Why there is no such clarification for reflection (jvms-5.5-110-C)?


Thanks,
Leonid


More information about the jls-jvms-spec-comments mailing list