"Hi, Amigo!"
"As you already know, anonymous inner classes can't have a constructor."
"Yeah. It's not very convenient. Constructors are really important."
"As the saying goes, nothing's impossible if you want it bad enough."
"Remember, static variables don't have static constructors, but there is a static initializer – the static block."
"Yeah, I remember."
"Anonymous inner classes have the exact same initializer, except it's not static."
class Car {
public ArrayListcreatePoliceCars(int count) { ArrayList result = new ArrayList(); for(int i = 0; i < count; i++) { final int number = i; result.add(new Car() { int policeNumber; { policeNumber = number; } }); } return result; }
}
"This time I highlighted the anonymous inner class code in red, and its initializer (in effect, its constructor) in purple. The 'constructor' has a body, but there's no method signature:"
Expectation | Reality |
---|---|
|
|
"You can declare variables inside such a class and initialize them in the initializer."
"Excellent, now their are far fewer restrictions."
"With that, we're getting to know inner classes. I hope you liked it."
"A lot. The lessons from Kim, Rishi, and especially you, Ellie, have been simply top notch."
"What a sweet talker! Keep going…"
GO TO FULL VERSION