Group Group Group Group Group Group Group Group Group

Why does field injection work with lateinit var but not with var?

Hi all!

In Chapter 7.2, field injection is performed in SequencePresenterImpl with:

@Inject
lateinit var sequenceModel: SequenceGenerator<Int>

Later, in Chapter 9.3 a similar thing is done with SequenceViewBinderImpl:

@Inject
var sequenceViewListener: SequenceViewBinder.Listener? = null

However, the latter throws the error: Dagger does not support injection into private fields.

So, why does the former work? Is it too silly a question?

Cheers :slight_smile:

Found the answer here:

When you use lateinit the generated field has the same visibility as its getter and setter, in this case public . This makes it work with Dagger.