https://kotlinlang.org logo
Title
i

Ive Vasiljevic

11/27/2019, 3:54 PM
What is the difference between:
class SomeClass1(private val property: Application)
and
class SomeClass2() {

   private val property: Application? = null
}
I've noticed that in the second example of property declaration you HAVE to initialize property otherwise the compiler says that property needs to be abstract or initialized, but on the other hand in the first example initialization is optional.
d

diesieben07

11/27/2019, 3:55 PM
The first one also declares a constructor with a parameter called
property
and initializes the property
property
with that parameter's value. The 2nd one declares a no-argument constructor
j

jw

11/27/2019, 3:55 PM
It's not optional in the first example. It's exposed as a constructor parameter whose value is assigned to the property.
e

E.Kisaragi

11/27/2019, 11:45 PM
if SomeClass1 has constructor(private val property: Application? = null), there's no differences Assignment null to property (especially val), it will be influenced Nothing? since it is bottom type So calling SomeClass2#property#someAppMethod is okay but never successful.