1

I have created an interface on kotlin.

interface IDataManager{
    val dataType: String?
}

Now I am trying to get its variable in my java class, like following.

public static DataWrapper getInstance(IDataManager iDataManager) {
    dataType= iDataManager.dataType;
    return instance;
}

But I am getting error: cannot find symbol iDataManager.dataType

2 Answers 2

3

Please call getter function to get a value of the variable:

dataType = iDataManager.getDataType();

If we use properties on Kotlin side we should use getters and setters to access those properties on Java side.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much !! :)
0

edit - as Alexey points out in the comments, this doesn't work for interfaces, since the property needs a backing field and properties in interfaces can't have those. It's still useful to know, but it doesn't apply to the OP's question

As well as what Sergey said, you can add the @JvmField annotation on things if you want to expose them as a field instead of generating the getters and setters

interface IDataManager{
    @JvmField val dataType: String?
}

@JvmStatic is another useful one for Java interop, you can put it on properties and functions in companion objects, so instead of this

Utils.Companion.coolUtility()

you can do this (like you're used to)

Utils.coolUtility()

2 Comments

@JvmField won't work on an interface property, because interfaces can't have instance fields.
Aww heck, you're right, sorry! It's good to be aware of them at least (for the situations where you actually can use them)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.