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

前端 未结 7 1736
暗喜
暗喜 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:42

    Make the B nested of A, like this:

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

    Or move B to a separate file. Also you can stick with default access level, this way the class can be accessed only from within the package:

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

提交回复
热议问题