Basically, that's what I'm trying to do:
ClassName { final OtherClass field; ClassName() { field = new OtherClass(this); } }
A final variable can only be initialized once, either via an initializer or an assignment statement.
Either declare the function as static in your class or move it outside the class altogether. If the field isn't final (which for best practice, it should be, unless the field has to mutate), you can initialize it using a regular non-static method in the constructor body.
It's not possible to assign a final field in a constructor body. The final field needs to be assigned before the constructor body, in the initializer list or on declaration:
class ClassName { final OtherClass field = new OtherClass(); // Here ClassName() : field = new OtherClass() // or here { } }
As you can't use this
in the initializer list or on the declaration, you can't do what you plan to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With