Questions tagged [kotlin-lateinit]

78 questions
81
votes
3 answers

isInitialized - Backing field of lateinit var is not accessible at this point

I'm trying to check if a lateinit property has been initialized. In Kotlin 1.2 we now have the isInitialized method for that. It works when I do that in the class where the lateinit property is declared. But when I try to call this from another…
ElegyD
  • 4,393
  • 3
  • 21
  • 37
49
votes
11 answers

Kotlin: lateinit to val, or, alternatively, a var that can set once

Just curious: In Kotlin, I would love to get some val that can be initialized by lazy, but with a parameter. That's because I need something that's created very late in order to initialize it. Specifically, I wish I had: private lateinit val…
Maneki Neko
  • 1,177
  • 1
  • 14
  • 24
25
votes
3 answers

Kotlin - How to make field read-only for external classes

I have the following Kotlin class on Android: class ThisApplication: Application() { lateinit var network: INetwork override fun onCreate() { super.onCreate() network = Network() } } Now, any external class can get…
22
votes
2 answers

How to inject primitive variables in Kotlin?

I am using Dagger2 for DI in my Android app, and using this code for injecting classes into my Activity is fine: @field:[Inject ApplicationContext] lateinit var context: Context but, lateinit modifier is not allowed on primitive type properties in…
15
votes
1 answer

How to uninitialize lateinit in Kotlin

I have a lateinit var as lateinit var someVariable: SomeVariable I initialize this value like someVariable = SomeVariable() and use it whenever I need. At a certain point, I want to set everything to default and want to "uninitialize" someVariable.…
musooff
  • 6,412
  • 3
  • 36
  • 65
12
votes
3 answers

How to hide Kotlin's lateinit var backing field from Java?

In Kotlin, suppose, I have class: class MyKotlinClass { lateinit var field: String } According to docs: Late-Initialized properties are also exposed as fields. The visibility of the field will be the same as the visibility of lateinit property…
Michael Spitsin
  • 2,539
  • 2
  • 19
  • 29
11
votes
1 answer

Check if a local lateinit variable is initialized

Member lateinit variables initialization can be checked with: class MyClass { lateinit var foo: Any ... fun doSomething() { if (::foo.isInitialized) { // Use foo } } } However this syntax doesn't work for…
Nicolas
  • 6,611
  • 3
  • 29
  • 73
11
votes
1 answer

lateinitVar cannot be resolved to use isInitialized from Kotlin 1.2.10

I want to use this feature the simplest thing like in the example does not work for me: lateinit val foo = 1 val bar = foo::lateinitVar.isInitialized() But I am getting unresolved reference lateinitVar I am using Kotlin 1.2.10 via gradle in…
ligi
  • 39,001
  • 44
  • 144
  • 244
8
votes
6 answers

Why late init var cannot be used with Nullable?

Why can't we use lateinit with nullable variables? lateinit var v: String? lateinit modifier is not allowed on properties of nullable types
Abraham Mathew
  • 2,029
  • 3
  • 21
  • 42
8
votes
4 answers

Is the keyword lateinit unnecessary?

I am in the process of learning Kotlin, and reading about the lateinit keyword makes me doubt its usefulness. Consider this code: var testString: String? = null lateinit var lateTestString: String fun print() { print(testString?.length) …
7
votes
2 answers

Notified when lateinit var has been initialised (Kotlin)

This is a straight forward question but I cannot find an answer. Is there a way to be notified when a lateinit var has been initialised in Kotlin? I know I can check if it has been initialised with this::coolvar.isInitialized but this is not the…
SARose
  • 3,558
  • 5
  • 39
  • 49
6
votes
2 answers

lateinit property not initialized when Activity is re-created

In my Activity I have a lateinit property called controller that my Fragment uses. This property is initialized in Activity.onCreate(). My Fragment gets its reference back to my Activity through onAttach(). The Fragment then calls…
dumazy
  • 13,857
  • 12
  • 66
  • 113
4
votes
2 answers

lateinit, lazy and singleton pattern in kotlin

I'm trying to convert some part of my project from java to kotlin. One of it is a singleton manager class. The java class looks like this public class Manager { private static volatile Manager Instance = null; private static final Object…
Yao
  • 709
  • 2
  • 11
  • 22
4
votes
3 answers

Kotlin with Spring DI: lateinit property has not been initialized

I don't get Spring-based setter dependency injection in Kotlin to work as it always terminates with the error message "lateinit property api has not been initialized". I could reduce the problem to the following scenario: There is an…
Patrick
  • 184
  • 1
  • 1
  • 12
3
votes
1 answer

How to check if a “lateinit” variable has been initialized inside extension method?

I want to check the lateinit propery initialized or not inside an extension method. I want to execute simple function call to lateinit property safely inside an extenstion method. I can use this::property.isInitialized. Want to write some extension…
1
2 3 4 5 6