Java Reference
In-Depth Information
}
}
This solution uses an
alternate constructor invocation
[JLS 8.8.7.1]. This feature allows one
constructor in a class to chain to another constructor in the same class. In this case,
MyThing()
chains to the private constructor
MyThing(int)
, which performs the required instance initialization.
Within the private constructor, the value of the expression
SomeOtherClass.func()
has been
captured in the parameter
i
and can be stored in the final field
param
after the superclass
constructor returns.
The
Private Constructor Capture
idiom illustrated by the solution to this puzzle is a useful pattern
to add to your bag of tricks. We've seen some genuinely ugly code that could have been avoided
with this pattern.
< Day Day Up >
Search WWH ::
Custom Search