Given the code below:
class Foo {
companion object {
lateinit var names: Array<String>
}
init {
names = arrayOf("George", "Keith", "Martha", "Margret")
}
}
If I created two instances of Foo
:
var foo1 = Foo();
var foo2 = Foo();
is names
going to be initialised/assigned twice, and therefore with any subsequent Foo
initialisation?
My intent is to have a simple static variable names
holding those predefined names.
Update:
This is assuming I do not want to have: var names: arrayOf("George", "Keith", "Martha", "Margret")
inside companion object.
is names going to be initialised/assigned twice, and therefore with any subsequent Foo initialisation?
As in init block its initializing names
every time so it will create new Array
for every instance.
You can verify this with simple print
class Foo {
companion object {
lateinit var names: Array<String>
}
init {
println("Creating Foo")
names = arrayOf("George", "Keith", "Martha", "Margret")
}
fun getHashCode():Int{
return names.hashCode()
}
}
fun main() {
var foo1 = Foo();
println(foo1.getHashCode());
var foo2 = Foo();
println(foo2.getHashCode());
}
In Output HashCode are different :
Creating Foo
746292446
Creating Foo
1072591677
You can try below code block to achieve what you want.
class Foo {
object ArrayOfString {
val names = arrayOf("George", "Keith", "Martha", "Margret")
}
companion object {
val names: Array<String> = ArrayOfString.names
}
}
By this way, you won't have initialization every time for your array, hope that make sense!
Companion object in Kotlin works same as the the static in java. By declaring a companion object inside our class, you'll be able to call its members with the same syntax as calling static methods in Java.And yes its going to be initialized once also in kotlin we have one more keyword for singleton instance to create the singleton in kotlin.
On other hand init is going to called after primary constructor also you can have one or more init blocks executing in series.
class Foo {
Var names :ArrayList<String> = arrayOf("George", "Keith", "Martha", "Margret")
companion object {
lateinit var names: Array<String>
}}
You can do like this
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.