What are the purposes of inner classes

前端 未结 10 1848
终归单人心
终归单人心 2020-11-29 23:14

I am reviewing the concept of inner classes in java. so far from what I\'ve understood and applied java inner classes has a link or access to the methods and fields of its o

10条回答
  •  旧巷少年郎
    2020-11-29 23:22

    Inner classes are best for the purpose of logically grouping classes that are used in one-place. For example, if you want to create class which is used by ONLY enclosing class, then it doesn't make sense to create a separate file for that. Instead you can add it as "inner class"

    As per java tutorial:

    Compelling reasons for using nested classes include the following:

    • It is a way of logically grouping classes that are only used in one place.
    • It increases encapsulation.
    • It can lead to more readable and maintainable code.

提交回复
热议问题