0

I am interesting are there any way to replace kotlin interface variable in Java.

E.g. what should i write in java to have same logic

interface ObjectWithId
{
    var id : Long
}

Before facing this problem all my code could be easily converted back to java, but using interface variables breaks 'backward compatibility'. Isn't it?

Bios90
  • 801
  • 2
  • 14
  • 26
  • I'm not clear if you're asking how to create an implementation of this specific interface in your Java code, or how to define an interface in Java with the same functionality as this one. – Tenfour04 Mar 29 '20 at 00:50
  • 1
    One helpful site for these question. It provides a kotlin compiler + java disassembler https://javap.yawk.at/#O0tyIG/procyon – k5_ Mar 29 '20 at 01:13

1 Answers1

4

In java this interface would be:

public interface ObjectWithId {
    long getId();        
    void setId(long p0);
}
Naman
  • 27,789
  • 26
  • 218
  • 353
k5_
  • 5,450
  • 2
  • 19
  • 27
  • Thanks for answer, and also i should have some variable in 'implementer' to store id? – Bios90 Mar 29 '20 at 01:05
  • 1
    This is the correct answer, the `var` or `val` in Kotlin are not actually variables, but properties (a setter, getter and backing field joined together). Interfaces are not supposed to hold state, see [this](https://stackoverflow.com/questions/2430756/why-are-interface-variables-static-and-final-by-default). – Eduardo macedo Mar 29 '20 at 01:05
  • 1
    @Bios90 thats open for the implementor, how this property is implemented. Most common would be a private field. – k5_ Mar 29 '20 at 01:07