java中提示此处不需要修饰符,单个文件中包含多个类:此处不允许使用修饰符private...

I am not able to understand why this code doesn't compile:

class A {

public static void main(String[] args) {

System.out.println("hi");

}

}

private class B {

int a;

}

I am saving the contents in a file named A.java - and I get an error:

modifier private not allowed here // where I have defined class B

This happens both when I try B as private and protected. Can someone please explain me the reason behind this?

Thanks !

解决方案

The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration

So yes, the private and the protected modifiers are not allowed for top level class declarations.

Top-level classes may be public or not, while private and protected are not allowed. If the c

你可能感兴趣的:(java中提示此处不需要修饰符)