Is it OK to reference 'this' when initializing a field?
- by parxier
Is it OK to reference this when initializing a field?
public class MainClass {
private SomeFieldClass field = new SomeFieldClass(this);
public MainClass() {}
}
Or is it better to do that in constructor?
public class MainClass {
private SomeFieldClass field;
public MainClass() {
this.field = new SomeFieldClass(this);
}
}
What is the best practice? I believe first option is better for unit testing and dependency injection. Are there any problems with it?