Java code showing error: "inner class can't be public"?

Java code showing error: "inner class can't be public"?

class one {
    void methodOne(){
               public class Two {

                }
    }
}

Java 代码显示错误:“内部 class 不能 public”?

在Java中,你可以在一个方法中写一个class。这些被称为 method-local inner classes 并且它们将是局部类型(非常像局部变量),内部 class 的范围被限制在方法内,所以它不能是 public (更多详情here and here)。

这是一个完整的例子:

public class MethodLocalInnerClassExample {
    private int x = 10;

    void printFromInner(final int a) {
        final int y = 10;

        class MethodLocalInnerClass {
            int w = 5;

            public void print(int z) {
                System.out.println("x + y - z + w + a = " + (x+y-z+w+a));
            }
        }

        MethodLocalInnerClass innerClass = new MethodLocalInnerClass(); // must come after the class' definition
        innerClass.print(5);
    }
}