Non-public top level class in Java

前端 未结 5 445
自闭症患者
自闭症患者 2021-01-03 03:28

What\'s the reason of making top-level class non-public in Java?

Let\'s say we have Foo.java, there could be

class Foo {
}
5条回答
  •  误落风尘
    2021-01-03 04:16

    I understand that there will be some class - visibility issues with the former example (probably it won't be visible from other packages).

    That seems to me to be reason enough to use it if you want to keep the class private to that one package.


    Just noticed another use! It seems you can only have one public top-level class per code file, but any number of non-public top-level classes. Haven't verified it personally, but if true that could be quite useful to prevent cluttering your project folder and to group classes with related functionality that aren't needed outside of the package.

提交回复
热议问题