Is it OK to pass this
as an argument to a method while constructing an object in java?
Thinking about doing this makes me feel uneasy, but I'm not sure if it's definitely wrong. Take the following hypothetical example:
public final class A {
private final B b;
private final List<String> words;
public A(B b) {
this.b = b;
words = new ArrayList<String>();
for(int i = 0; i < 10; i++) {
words.add(b.getNextStringFromUser(this));
}
}
public List<String> getWords() {
return words;
}
}
public class B {
// returns a String chosen by the user based on the current state of A
public String getNextStringFromUser(A a) {
List<String> wordsSoFar = a.getWords();
// ... omitted
}
}
The cases I can think of where doing this might be the right thing to do is where you want to construct an object which can be immutable from the point of view of the rest of your code, but where the constructor might take a different course depending on the state specified so far (if it makes sense to talk about the state of a partly constructed object). In the example above, a user chooses a string based on the strings chosen so far, and when they're all chosen, this object should never change again.
Is this sort of thing OK/advisable? Thanks.