I have a java interface from a third party with st...
# getting-started
r
I have a java interface from a third party with static fields that I would like to inherit in a kotlin interface/class/object so I can access all of them statically. Can/how/should I do this?
here is the java equivalent
Copy code
// ThirdPartyInterface.java
public interface ThirdPartyInterface {
    public static final String HELLO = "hello";
}

// MyConstants.java
public class MyConstants {
    public interface HelloWorld extends ThirdPartyInterface {
        public static final String WORLD = "world";
    }
    public static class HelloWorldExclamation implements HelloWorld {
        public static final String EXCLAMATION = "!";
    }
}

println(ThirdPartyInterface.HELLO)
println(MyConstants.HelloWorld.HELLO)
println(MyConstants.HelloWorld.WORLD)
println(MyConstants.HelloWorldExclamation.HELLO)
println(MyConstants.HelloWorldExclamation.WORLD)
println(MyConstants.HelloWorldExclamation.EXCLAMATION)
optimally I would like
MyConstants.java
to be converted to kotlin.
I haven't actually tried the convert java to kotlin tool in intellij yet since I'm writing this from scratch, I'll give that a shot first and maybe answer my own question lol
j
How can
HELLO
be available in
MyConstants.HelloWorld
? Did you forget inheritance relationships somewhere in the sample code?
r
ope, sure did, thanks, just fixed it
j
Now, aside from that, statics are not really supposed to be accessible like that. Even in Java, linters usually report such accesses as a code smell. Why do you want those accessible from your own classes?
r
I'm defining a set of contracts and each contract has to adhere to some common contract, so it's more of a convenience thing so developers don't have to re-write the constants or reference the base/common contract directly
Copy code
object HelloWorld {
    // would like to eliminate having to add this manually
    const val HELLO = BaseContract.HELLO
    const val WORLD = "world"
}

// preferably would like to reference `HelloWorld` here instead
put(BaseContract.HELLO)
put(HelloWorld.WORLD)
🤔 1
interesting, if I convert the working java code to kotlin it actually changes the reference from my interface to the third party interface
Copy code
System.out.println(HelloWorld.HELLO);
// changes to
println(ThirdPartyInterface.HELLO)