Why can't I access a variable declared in a class that implements the Java interface from Scala?

In Java, I have a class that implements an interface:

AlertDialog implements DialogInterface 

If some variables are declared inside the interface, I can access them:

 AlertDialog.BUTTON_POSITIVE 

But in Scala, the specified string does not compile. It appears to be hidden. Is there a way to access these variables in Scala without creating a new object or doing something else hacked?

+7
source share
2 answers

There is no way in Scala to access these variables from the AlertDialog class, but you can use the interface itself as an object to access them.

Thus, you can directly access variables from the interface:

 DialogInterface.BUTTON_POSITIVE 
+2
source

To give a little more detailed information: the reason they cannot be achieved is because George is talking about static members defined on an interface. Scala does not contain static elements - instead, an object is created, which is a regular class with one implementation. When you expand from the Java interface, Scala will only distribute non-static elements, because static are treated as being in a companion object . The companion object is called the same as the interface, so you can access it as DialogInterface.BUTTON_POSITIVE .

+14
source

All Articles