0

I have a Java interface, and need to access it throught my Kotlin application. But it is not working.

// On Java

public interface IMyInterface {
    int TEST_OK = 1;

    enum MyEnum {
        NOK(0),
        OK(1);

        private int val;
        MyEnum(int val) {
            this.val = val;
    }
}
public final class MyClass implements IMyInterface {
...
}

// And on Kotlin

MyClass.TEST_OK // Works
MyClass.MyEnum.OK // Does not work (Unresolved reference)

IMyInterface.MyEnum.OK // Works

Any lighting?

5
  • There is another interesting behavior, through MyInterface class, I can only access static methods. I can't understand why. Commented Aug 12, 2020 at 15:14
  • Update: Although I can't access the enum through the class MyInterface, I can access it through the interface itself ( IMyInterface.MyEnum.OK ). Commented Aug 12, 2020 at 15:18
  • What do you mean "through the class MyInterface"? It isn't a class. Commented Aug 12, 2020 at 15:28
  • In the example above, MyInterface is a class that implements IMyInterface. The name are terrible, sorry. I will edit it. Commented Aug 12, 2020 at 15:38
  • 1
    MyClass.TEST_OK should not work. Commented Aug 12, 2020 at 16:06

1 Answer 1

2

Implementing an interface does not give the implementing class direct access to the interface's static members like the implicitly static int TEST_OK or the static inner class MyEnum.

In Java, static members belong to a class object with the same name as the class or interface they were defined in, and are treated distinctly. The act of implementing the interface is completely distinct from any static members of that interface.

I suspect this is one of the reasons Kotlin's designers did not carry over the concept of static members and instead replaced it with companion objects. The concept of the class vs. the class object that has all the static members is confusing.

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

Comments

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.