When to use inner classes in Java for helper classes

后端 未结 11 2131
醉梦人生
醉梦人生 2020-12-04 07:24

If I have for example a class along with a helper class to do some of its functionality, does it make sense to make it as an inner class.

    public class F         


        
11条回答
  •  长情又很酷
    2020-12-04 07:56

    If you think that FooHelper will not at all be useful for other classes than Foo, then it makes sense to make it as private inner class of Foo. One example of this kind of design can be found in HashMap where it defines a private inner class KeySet

    Otherwise having it as a private instance looks good.

提交回复
热议问题