Pros and cons of package private classes in Java?

前端 未结 8 1794
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 05:05

I am learning Java recently, and I came across the notion of package-private classes, which is the default if we don\'t specify anything. But then I realized:

8条回答
  •  迷失自我
    2020-11-27 05:20

    Please notice that when you are speaking about classes you have only two options:

    1. public classes
    2. package private classes

    The concept of "private class" is meaningless. (Why to make a class that is not used anywhere?!)

    So if you have a class for intermediate operations that needn't to be exposed to API users you are supposed to declare it as "package private"

    Also when you define many classes in the same source file, only one class is allowed to be public (its name matched the .java file name). If any other class is defined in the same file it must be "package private".

提交回复
热议问题