Non-public top level class in Java

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

    Classes without a public or protected modifier are only visible inside the package they reside. If you think of components and interfaces there is a reason for leaving out the public modifier. Let's say you have a public class MyCompontent that internally uses other classes, but does not want to publish those to the outside world (users of the component) it makes sense to leave out the visibility modifier.

提交回复
热议问题