Lets say I have a concrete class Class1 and I am creating an anonymous class out of it.
Object a = new Class1(){ void someNewMethod(){ } };
Now is there any way I could overload the constructor of this anonymous class. Like shown below
Object a = new Class1(){ void someNewMethod(){ } public XXXXXXXX(int a){ super(); System.out.println(a); } };
With something at xxxxxxxx to name the constructor?
From the Java Language Specification, section 15.9.5.1:
Sorry 🙁
EDIT: As an alternative, you can create some final local variables, and/or include an instance initializer in the anonymous class. For example:
It’s grotty, but it might just help you. Alternatively, use a proper nested class 🙂