Naming convention for utility classes in Java

前端 未结 5 1862
离开以前
离开以前 2021-01-29 21:53

When writing utility classes in Java, what are some good guidelines to follow?

Should packges be \"util\" or \"utils\"? Is it ClassUtil or ClassUtils? When is a class a

5条回答
  •  误落风尘
    2021-01-29 22:29

    There is no standard rule/convention in Java world for this. However, I prefer adding "s" at the end of Class name as @colinD has mentioned.

    That seems pretty standard to what Master java API Designer Josh Bloch does ( java collection as well as google collection)

    As long as Helper and Util goes, I will call something a Helper when it has APIs that help to achieve a specific functionality of a package ( considering a package as to implement a module); mean while a Util may be called in any context.

    For example in an application related to bank accounts, all number specific utility static APIs would go to org.mycompany.util.Numbers

    All "Account" specific business rule helping APIs would go to

    org.mycompany.account.AccountHelper
    

    After all, it is a matter of providing better documentation and cleaner code.

提交回复
热议问题