0

I'd like to have a generic public field inside an interface, like this:

interface Interface<T> {
    T field;
}

However, the following throws with:

'com.packageName.Interface.this' cannot be referenced from a static context

Why?

kiafiore
  • 1,071
  • 2
  • 11
  • 27
John Smith
  • 3,863
  • 3
  • 24
  • 42
  • That's not gonna work, since all interface fields are implicitly `public static final`. You're going to have to like to do something that's allowed by the language. In other words, you're going to have to design your code so that it can actually be written. – Kayaman Nov 21 '20 at 12:03
  • [Why are interface variables static and final by default?](https://stackoverflow.com/q/2430756/12323248) – akuzminykh Nov 21 '20 at 12:05
  • Define getter and setter instead, that's what kotlin does behind the scenes actually. – Animesh Sahu Nov 21 '20 at 12:06

0 Answers0