When to use inner classes in Java for helper classes

后端 未结 11 2053
醉梦人生
醉梦人生 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.

    0 讨论(0)
  • 2020-12-04 07:36

    From JAVA SE Docs

    Why Use Nested Classes?

    It is a way of logically grouping classes that are only used in one place: 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.

    It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.

    So Yes, it makes sense to use FooHelper as an inner class.

    0 讨论(0)
  • 2020-12-04 07:37

    Hild, Yes, it makes sense to use an inner class in many cases.

    Think of it this way - the inner class will live and die with the outer class, so any functionality that is specifically needed for the outer class can be added to the inner class. Popular examples are - Listeners in most cases - Types of KeyListeners, MouseListeners, eventListeners.

    Classes in Java allow you to have specific functionality, but sometimes you may need to have a separate specialized functionality but it also needs to be intimately tied to the class you're designing.

    There can be four types of inner classes. A simple google search can help you to find out more about them.

    0 讨论(0)
  • 2020-12-04 07:37

    Nested Classes,enable you to logically group classes that are only used in one place, increase the use of encapsulation, and create more readable and maintainable code. Local classes, anonymous classes.

    http://docs.oracle.com/javase/tutorial/java/javaOO/whentouse.html

    0 讨论(0)
  • 2020-12-04 07:40

    What is Foo's scope? When Foo is domain model class with it's own lifecycle and helper is common service, seems like mixing of two objects with very different scope/lifecycle.

    Typically domain entity has it's own lifecycle from creation, persistence to it's GC. On the other hand, helper or service is either static or better dynamic with lifecycle equals to the whole app, e.g. spring bean.

    Once your domain entity would contain reference of a service it can bring you serious problems. E.g. every call to repository's Get needs to inject reference of this service into domain entity. I'd recommend to avoid this pattern.

    It's not apparent for me who will make instance of Helper for you.

    0 讨论(0)
  • 2020-12-04 07:41

    Yes, the advantage of using inner class is it can access members of outer class.In your case , if you think your FooHelper is not to be used by any other class,you can make it a inner class.

    To check out the utility of inner class, go through the examples of AWT. Anonymous inner classes are widely used in event handlers.

    0 讨论(0)
提交回复
热议问题