When to use inner classes in Java for helper classes

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

提交回复
热议问题