Class member variables can have a lateinit
property. It signifies a non-null member variable that is not initialised in a constructor but will be initialised later. The initialisation can happen, for example, in unit test setup functions.
public class Test {
lateinit var foo: Foo
@SetUp fun setup() {
foo = GetFoo()
}
@Test fun test() {
foo.method()
}
}
When a uninitialised lateinit
variable is accessed, a special exception is thrown. We can also check if the variable has been initialised or not. In the above example, we can use Test::foo.isInitialized