the main method in an interface

Alex Buckley alex.buckley at oracle.com
Fri Mar 13 21:12:40 UTC 2015


Hi Leonid,

On 3/12/2015 4:43 PM, Leonid Kuskov wrote:
> 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).

Thanks for spotting that we really support an initial class _or 
interface_. I confirmed with Brian Goetz that this is intentional, and 
have filed https://bugs.openjdk.java.net/browse/JDK-8075167.

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

There are six clauses under the umbrella "only as a result of" 
assertion; only the first says "if it has not been initialized already." 
That's both redundant (since the initialization algorithm itself doesn't 
reinitialize) and potentially confusing (because it can be read as 
implying that initialization always succeeds). I have filed 
https://bugs.openjdk.java.net/browse/JDK-8075168. I think the question 
about why the reflective clause doesn't say it is now moot (and in any 
case the JVMS tries to make very few commitments about the behavior of 
reflection).

Alex


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