In kotlin, how to make the setter of properties in primary constructor private?
Date : March 29 2020, 07:55 AM
like below fixes the issue The solution is to create a property outside of constructor and set setter's visibility. class Sample(var id: Int, name: String) {
var name: String = name
private set
}
|
Kotlin - Factory Function for Class with Private Constructor
Date : March 29 2020, 07:55 AM
it fixes the issue In Kotlin, is it possible to have a factory function that creates an instance of a class with a private constructor? , You can do something like this: import MyClass.Companion.myClassOf
class MyClass private constructor() {
companion object {
fun myClassOf() = MyClass()
}
}
//val myInstance1 = MyClass() // not allowed
val myInstance2 = myClassOf()
|
What's the difference when adding a private modifier to arguments in a Kotlin's constructor?
Date : March 29 2020, 07:55 AM
hop of those help? I'm not sure about the difference between the following two constructors written in Kotlin , The following code: class ConcreteItem(val title: String) : Item() {
}
class ConcreteItem(title: String) : Item() {
val title: String = title
}
class ConcreteItem(title: String) : Item() {
public val title: String = title
}
class ConcreteItem(private val title: String) : Item() {
}
class ConcreteItem(title: String) : Item() {
private val title: String = title
}
|
Kotlin syntactic sugar: make var all private in constructor
Date : March 29 2020, 07:55 AM
wish helps you There's no way to do this currently. The default visibility is public, and you can only change it on a per-property basis. Perhaps your class could implement an interface that doesn't expose all these properties, and you could pass instances of it to client code as that type - although I don't know your exact situation and requirements.
|
Kotlin Singletons: Object vs a Class with private constructor
Tag : kotlin , By : user183825
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Using an object is an issue if your singleton instance needs parameters, like in this case here, with GardenPlantingDao, as they cannot take constructor arguments. This comes up frequently on Android, as there's many cases where singletons requires a Context to operate. You could still use an object in these cases, but it would either be unsafe or inconvenient:
|