Java – does the member interface in the class declaration imply public?

code

I have a class with a member interface:

package com.example.withinterface;

public class SomeClass {

    interface SomeInterface {

        void doSomething();
    }
}

Another tried to access its course:

package com.example.withinterface.main;

import com.example.withinterface.someClass;

public class Main {

    public static void main(String[] argss) {
        System.out.println(SomeClass.someInterface.class);
    }
}

error

In main, I get the following error from javac: someinterface in someclass is not public; Cannot be accessed from the outer wrapper

In Eclipse: someinterface is not public in someclass; Cannot be accessed from the outer wrapper

Both are compiled into Java 7 If I expose someinterface, all the compilation is good

But the specifications say

The Java language specification for Java 7 says:

There seems to be no second sentence in the Java language specification for Java 5

topic

So someinterface should not be considered public and should not be mainly compiled?

to update

As Ajay George shows, this is indeed an error in the Java language specification 7 (thanks to James B) At the same time, the specification is modified and the wrong sentence is deleted Last version in Archive. org with the incorrect sentence.

Solution

I guess this specification is wrong

E:\workspace>javap com\example\withinterface\SomeClass
Warning: Binary file com\example\withinterface\SomeClass contains com.example.wi
thinterface.someClass
Compiled from "SomeClass.java"
public class com.example.withinterface.someClass {
  public com.example.withinterface.someClass();
}

E:\workspace>javap com\example\withinterface\SomeClass$SomeInterface
Warning: Binary file com\example\withinterface\SomeClass$SomeInterface contains
com.example.withinterface.someClass$SomeInterface
Compiled from "SomeClass.java"
interface com.example.withinterface.someClass$SomeInterface {
  public abstract void doSomething();
}

I changed the interface to public and recompiled it

E:\workspace>javap com\example\withinterface\SomeClass
Warning: Binary file com\example\withinterface\SomeClass contains com.example.wi
thinterface.someClass
Compiled from "SomeClass.java"
public class com.example.withinterface.someClass {
  public com.example.withinterface.someClass();
}

E:\workspace>javap com\example\withinterface\SomeClass$SomeInterface
Warning: Binary file com\example\withinterface\SomeClass$SomeInterface contains
com.example.withinterface.someClass$SomeInterface
Compiled from "SomeClass.java"
public interface com.example.withinterface.someClass$SomeInterface {
  public abstract void doSomething();
}

Pay attention to the differences of inner classes

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>