Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 520
deltanovember Avatar asked Jul 15 '11 06:07

deltanovember


1 Answers

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.

like image 71
rxg Avatar answered Oct 18 '22 16:10

rxg