Skip to content
Advertisement

Creating a Java Enum in Scala

My workplace has been experimenting in moving from Java to Scala for some tasks, and it works well for what we’re doing. However, some preexisting logging methods expect a java.lang.Enum. The logging method is defined in the (Java) base class, and the subclasses can define their own enums, which the logger will track across all instances in multiple threads/machines.

It works like this in Java:

JavaScript

Then, when the task has finished, we can see that

JavaScript

Is there any way to replicate this in Scala, either by creating Java Enums or converting from Enumeration to Enum? I have tried extending Enum directly, but it seems to be sealed, as I get the error in the console:

JavaScript

Advertisement

Answer

If you need a java enumeration, then you need to write it in Java. There are things you can do in Scala to replace the use cases of Enum, but there’s nothing in Scala that replicates the Java mechanics of Enum.

Advertisement