Passing `this` as an argument when building in java

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 { // 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 } } 

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.

+7
java constructor this
source share
5 answers

as a method argument when building an object in java?

I would think twice since your object is not even constructed fully .

It may have some attributes which are still null or unitialized. , and if any other class refers to those attributes that would be null, be prepared for NULL POINTER EXCEPTIONS .

Hope this helps.

+2
source share

Leaking the "this" link in the constructor is dangerous, especially in a multi-threaded environment. You may receive an incompletely constructed object that is visible to other threads. It can behave normally if used from a single thread, but errors of this kind are very difficult to find and fix. So don't be surprised if your IDE marks this as a warning.

+2
source share

The explicit Java language builds classes. See the specification here . If you are sure that the called code has access only to the state that is being initialized, then it is probably safe.

+2
source share

In the scenario, as you presented it, this is unacceptable.

For security reasons, you should try to avoid passing as many uninitialized / partially initialized objects as possible. In your case, you can re-declare getNextStringFromUser(A a) as follows:

 public String getNextStringFromUser(List<String> wordsSoFar) { // ... omitted } 

In addition, the scenarios in which you have two objects that must reference each other are not common. It may be useful to check the MVC design pattern and the Factory design pattern .

This is a similar question, except that only one object has a link to another.

+1
source share

The problem in your code does not pass this another method, the problem is that you are doing this in the constructor, which means that another object will be available to your class before the initialization is complete.

0
source share

All Articles