deltanovember
deltanovember

Reputation: 44091

How do I access Java enums from Scala?

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 enclosing class class ScalaEnum in package XXX is not a subclass of object Test in package YYY where target is defined

Upvotes: 8

Views: 4052

Answers (2)

rxg
rxg

Reputation: 3982

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.

Upvotes: 2

Alex Cruise
Alex Cruise

Reputation: 7979

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.

Upvotes: 0

Related Questions