Non-public top level class in Java

前端 未结 5 403
自闭症患者
自闭症患者 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:21

    Typically, you make a class package-private because you don't want the class to be used outside the package. When a top-level class isn't public, it's private to the package.

    Say you have a package with a number of classes that must communicate the same sort of data with one another. But this data structure is an implementation detail and so you don't want it being used by user code. Making the transfer class package private maintains this sort of package level encapsulation.

提交回复
热议问题