Is it possible to pass this as an argument to a method when constructing an object in java?
Thinking about what it does makes me feel awkward, but I'm not sure if this is 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 {
Cases that I can think of where this might be right are where you want to build an object that can be immutable from the point of view of the rest of your code, but where the constructor may take a different course depending on the state indicated so far then (if it makes sense to talk about the state of a partially constructed object). In the above example, the user selects a row based on the rows so far selected, and when they are all selected, this object should no longer change.
Is this something like OK / appropriate? Thanks.
java constructor this
user1675642
source share