You focus too much on low-level irrelevant details. At the byte code level, there may be a constructor that takes an int parameter, but at the language level you did not specify an explicit constructor, therefore, there will be a default constructor without any arguments, as with any other class.
This should become clear when you write pre-Java 8 code:
static Callable<Object> gen(int i) { class X { int x = i; public String toString() { return "" + x; } } X x=new X(); โฆ
You create an instance of X by its default constructor without accepting any arguments. Your local class captures the value of i , but how it is done at a low level, i.e. That the constructor X has a synthetic parameter int , and the expression new passes the value i it, is an implementation detail.
You can even add an explicit constructor as
X() {}
without changing anything.
Obviously, you can also write the new X() expression inside the lambda expression here, since the expressions do not change their semantics when placed inside the lambda expression:
return () -> new X();
or use its short form, method link
return X::new;
This is nothing special, the behavior is clear even without reference to the specification, if you forget about the distracting details of the low level. X can write as many local variables as you like, the number of constructors does not change (at the language level).
source share