开发者

How do I access Java enums from Scala?

开发者 https://www.devze.com 2023-03-20 06:44 出处:网络
My java class is as follows public class Test { protected enum TestEnum {A, B, C}; public Test(TestEnum te) {

My java class is as follows

public class Test {

    protected enum TestEnum {A, B, C};

    public Test(TestEnum te) {

    }

}

here is my Scala

class ScalaEnum(myEnum: TestEnum) extends Test(myEnum) {

}

I receive the following error message

class TestEnum in object Test cannot be accessed in object Test Access to protected class TestEnum not permitted because en开发者_JAVA百科closing class class ScalaEnum in package XXX is not a subclass of object Test in package YYY where target is defined


As @Alex and @Jean-Phillipe said, this has not much to do with the fact that you're trying to access an enum and more to do with the fact that inner-class enums are implicitly static: see this answer, for example.

That means that you're running up against this limitation. Changing TestEnum to be public works around the problem for me with Scala 2.9.1.

Having said all that, despite Martin's vehement objections to removing the limitation, your code works as expected with Scala 2.10.


It sounds like the enum class is implicitly static, because Scala calls it "object Test". Try qualifying it in the constructor (e.g. Test.TestEnum), and if that doesn't work, relaxing the visibility to package access might.

0

精彩评论

暂无评论...
验证码 换一张
取 消