When to use inner classes in Java for helper classes

后端 未结 11 2111
醉梦人生
醉梦人生 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:32

    Yes, it makes perfect sense to make it an inner class. If no other classes need it, make it private. If it doesn't require exclusive access to the members of the outer class, make it a static nested class because then it will require less memory space.

    Check out the recommendation from the official tutorial -

    Use a non-static nested class (or inner class) if you require access to an enclosing instance's non-public fields and methods. Use a static nested class if you don't require this access.

提交回复
热议问题