Issue
I have a class which impliments both the java.io.Serializable
and android.os.Parcelable
.
These classes require companion objects of:
companion object CREATOR : Parcelable.Creator<MyClass> {
override fun createFromParcel(parcel: Parcel): MyClass
...
}
and
companion object {
private val serialVersionUid: Long = 123
}
The trouble is that I can't have both these companion objects because that causes a only one companion object per class
exception.
How can I have two companion objects with different names in the same class?
Solution
May be you misunderstood Java examples.
public static Parcelable.Creator<SDFileDir> CREATOR = ...;
public static long serialVersionUid = 123;
In Java - yes, it is separated static object. You can place any count of static fields in class.
In Kotlin there should be only one static object (it is called Companion
here). But it is like one more class here. So all new static fields should be inside of it.
companion object {
@JvmField
val CREATOR: Parcelable.Creator<SDFileDir> = ...
val serialVersionUid: Long = 123
}
There is one more thing: annotation @JvmField
to work with Java correctly.
Answered By - Ircover
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.