Java compilation of a .java file without a public class

后端 未结 6 2017
被撕碎了的回忆
被撕碎了的回忆 2020-12-19 06:01

Okay, so a java source file must have at least one public class and the file should be called \"class-name.java\". Fair enough.

Hence, if I have a class, th

6条回答
  •  情书的邮戳
    2020-12-19 06:14

    You can place non-public class in a file, and it's not a bug but feature.

    Your problem is on level of packaging, not compile. Because you can compile this file with non-public class, but you can't call it from outside, so it's not working as application base class

    Like this:

    // [+] single file: SomeWrapper.java 
    
    public class SomeWrapper {
        ArrayList<_PrivateDataType> pdt;
    }
    // [-] single file: SomeWrapper.java 
    
    // [+] single file: _PrivateDataType.java 
    class _PrivateDataType {
        // members, functions, whatever goes here
    }
    
    // [-] single file: _PrivateDataType.java 
    

提交回复
热议问题