Passing final variables to anonymous classes

后端 未结 2 1395
我寻月下人不归
我寻月下人不归 2020-12-29 06:51

In final variable passed to anonymous class via constructor, Jon Skeet mentioned that variables are passed to the anonymous class instance via an auto-generated constructor.

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 07:41

    In this case, it's because 100 is a constant. That gets baked into your class.

    If you change x to be:

    final int x = args.length;
    

    ... then you'll see Test$1(int) in the output. (This is despite it not being explicitly declared. And yes, capturing more variables adds parameters to the constructor.)

提交回复
热议问题