0

I have a an Interface in Java with the static variable interfaceValue that I could access as below

public class Experimenting {

    public interface MyInteface {
        int interfaceValue = 10;
    }

    class MyImpl implements MyInteface { }

    MyImpl myImpl = new MyImpl();

    void testing() {
        int getInterfaceValue = myImpl.interfaceValue;
    }
}

When I convert to Kotlin, it is as below

class Experimenting {

    internal var myImpl = MyImpl()

    interface MyInteface {
        companion object {
            val interfaceValue = 10
        }
    }

    internal inner class MyImpl : MyInteface

    internal fun testing() {
        val getInterfaceValue = myImpl.interfaceValue
    }
}

However the myImpl.interfaceValue is showing compile error sign, where by it doesn't recognize the interfaceValue. How could I still access my interfaceValue in Kotlin?

2
  • declare variables in interface is ugly and bad practice Commented Oct 24, 2018 at 13:01
  • Thanks @Eugene. I'm just experimenting the language feature. Commented Oct 24, 2018 at 22:06

2 Answers 2

1

The conversion converts the static final interfaceValue to a val in the companion Kotlins equivalent of that. Unfortunately the converter is not perfect and sometimes messes thing up. To access it

import com.your.package.Experimenting.MyInteface.Companion.interfaceValue    
class Experimenting {

    internal var myImpl = MyImpl()

    interface MyInteface {
        companion object {
           const val interfaceValue = 10
        }
    }

    internal inner class MyImpl : MyInteface

    internal fun testing() {
        val getInterfaceValue = interfaceValue
    } 
}

does the trick.

also does:

class Experimenting {

    internal var myImpl = MyImpl()

    interface MyInteface {
        companion object {
           val interfaceValue = 10
        }
    }

    internal inner class MyImpl : MyInteface

    internal fun testing() {
        val getInterfaceValue = MyInteface.interfaceValue
    }
}

A third way would be to copy the value of interfaceValue into the implementing class:

class Experimenting {

    internal var myImpl = MyImpl()

    interface MyInteface {
        companion object {
            const val interfaceValue = 10
        }
    }

    internal inner class MyImpl : MyInteface{
        val interfaceValue = MyInteface.interfaceValue
    }

    internal fun testing() {
        val getInterfaceValue = myImpl.interfaceValue
    }
}

Basically you access it like you would access a static variable in java.

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

5 Comments

Thanks. I like your answer, but that makes the interfaceValue no longer accessible from the implementor, which makes it different from what it was originally intended. I still give you a vote for the syntactically correctness of it. Thanks!
I have another answer, which I think mimic the original behavior. stackoverflow.com/a/52978332/3286489 Do check it out and let me think what your thought is. Thanks.
Your answer loosens the constraints on interfaceValue from final to open. Also It will no longer be static.
Agree, it is no longer syntatically correct. Though it is not final, it is not mutable too. Hence in practical, the usage would be more similar to the original Java version.
final in the sense that you cannot override or change it. You can not change the value of interfaceValue in the java interface either
0

I guess the right conversion of such Java code to Kotlin code should be as below

class Experimenting {

    internal var myImpl = MyImpl()

    interface MyInteface {

        val interfaceValue: Int
        get() = 10
    }

    internal inner class MyImpl : MyInteface

    internal fun testing() {
        val getInterfaceValue = myImpl.interfaceValue
    }
}

Though syntactically they are different, i.e. interfaceValue not really a static variable anymore, but practicality the same.

8 Comments

What this lets you do is override the interfaceValue. If this is what you want go for it. It really depends if the interfaceValue is constant for all MyInterfaces (my answer and the behaviout as it is in java) or should be able to be overridden (this).
consider internal inner class MyMyImpl : MyInteface{ override val interfaceValue : Int get() = 11 }
I see, one could override it. You have the point, that the subtle different.
This is the kind of difference that causes hard to fix bugs. So if you do this do not assume it to be equal to the java code.
I guess in short Java != Kotlin, and Kotlin != Java. Both have its pro con.
|

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.