What are the purposes of inner classes

前端 未结 10 1875
终归单人心
终归单人心 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:34

    1. See the Java tutorial for the main reasons.

    2. If by "helper class" you mean something for internal use only, then no, not necessarily. You might want to do something like

      class Outer {
          private static class Inner implements InterestingInterface {
              // whatever
          }
          public InterestingInterface make_something_interesting() {
              return new Inner();
          }
      }
      

      Here, Inner is not a "helper class" in the sense that the outside world does get to see instances of it, but its implementation is entirely hidden -- the outside world only knows it gets some object that implements InterestingInterface.

提交回复
热议问题