Scala can't access Java inner class?

女生的网名这么多〃 提交于 2019-11-29 07:27:37

问题


I have a java class that looks like this:

public class Constants {
    public class Commands {
        public static final String CreateOrder = "CreateOrder";
    }
}

I want to access "CreateOrder" constant, In java I can access it easily like this:

String co = Constants.Commands.CreateOrder

But in Scala this doesn't work why??? How can I access "CreateOrder" from Scala, I can't alter the Java code.

Thanks.


回答1:


As far as I know, there is no way to do what you want in Scala.

But if you absolutely cannot change the Java code then you could resort to some reflection trickery.

val value = classOf[Constants#Commands].getDeclaredField("CreateOrder").get(null)



回答2:


From the Java Language Specification:

Inner classes may not declare static members, unless they are constant variables (§4.12.4), or a compile-time error occurs.

In the case of constant variables, you can use the Constants.Commands.CreateOrder syntax even though normally any reference to Constants.Commands would have to be associated with an instance of Constants, since it's not a static inner class.

This could be considered something of a crazy one-off syntactic special case in Java, and it's the kind of thing that the Scala language designers haven't bothered to bake into Scala's Java interoperability.

Your best bet (if your real Java class looks exactly like this) is to create an instance of Constants, at which point you can access the inner class's static field in a perfectly natural way:

scala> (new Constants).Commands.CreateOrder
res0: String = CreateOrder

If for some reason this isn't an option you'll have to go with the reflection-based approach in the other answer, unfortunately.




回答3:


I think a third alternative to reflection and instantiation is to write a small piece of Java glue code. Like

public class Glue {
    public static String createOrder() { return Constants.Commands.CreateOrder; }
}

Then from Scala you should be able to write Glue.createOrder.



来源:https://stackoverflow.com/questions/19345798/scala-cant-access-java-inner-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!