multiple classes in a single file : modifier priva

2020-08-09 07:06发布

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 !

7条回答
做个烂人
2楼-- · 2020-08-09 08:03

B needs to be private to something. Place it within the definition of class A or create another file, B.java, and define it there, but then it cannot be private.

查看更多
登录 后发表回答