Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Java I have a class that implements an interface:

AlertDialog implements DialogInterface

If some variables are declared inside of the interface I could access them:

AlertDialog.BUTTON_POSITIVE

But in Scala the above line does not compile. Seems like it is hidden. Is there any way to access these variables in Scala without creating a new object or doing anything else hacky?

like image 793
George Pligoropoulos Avatar asked Nov 12 '12 15:11

George Pligoropoulos


2 Answers

To give slightly more detail: the reason these can't be accesed is that George is talking about static members defined on the interface. Scala doesn't have static members - instead, one creates an object, which is a regular class with a single implementation. When you're extending from a Java interface, Scala will extend only the non-static members, because the static ones are treated as being in a companion object. The companion object is named the same as the interface, so you can access it as DialogInterface.BUTTON_POSITIVE.

like image 122
Submonoid Avatar answered Nov 08 '22 07:11

Submonoid


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

So you can directly access the variables from the interface:

DialogInterface.BUTTON_POSITIVE
like image 39
George Pligoropoulos Avatar answered Nov 08 '22 08:11

George Pligoropoulos