multiple classes in a single file : modifier private not allowed here

前端 未结 7 1750
暗喜
暗喜 2021-02-12 14:38

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

class A {
    public static void main(String[] args) {
        System.out.println(\"hi\");
    }
}

p         


        
7条回答
  •  借酒劲吻你
    2021-02-12 15:28

    If you don't use the public keyword for the class it will be private by default (visible only within the file).

    There can be only one public class per .java file, all the others need to be private. So class A can be public and class B doesn't need any modifiers in your example. The public class name must match the .java file name (eg. A.java can only contain one public class called "A").

提交回复
热议问题