Non-public top-level class vs static nested class

前端 未结 4 1056
情书的邮戳
情书的邮戳 2020-12-14 09:29

It seems to me that non-public top-level classes and static nested classes essentially perform the same tasks when creating a helper class.


A.java


<
4条回答
  •  有刺的猬
    2020-12-14 09:44

    In neither example do you have one class per source file. But generally, you use a static nested class to signify that it is only intended to be used within its enclosing class (forcing it to be referenced as A.AHelper). That is not so clear if you move that class to the top level.

    From the Sun tutorial:

    Logical grouping of classes—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.

提交回复
热议问题