Using Scala Methods from Java

Scala provides full round-trip interoperability with Java. Because Scala compiles to bytecode, you can use Scala classes from Java quite easily. Remember, Scala does not by default follow the JavaBean convention, and you’ll have to use the @scala.reflect.BeanProperty annotation to generate getters and setters that conform to the JavaBean convention—see Defining Fields, Methods, and Constructors. Let’s look at how to use a method defined in Scala from Java.

Scala classes that follow standard Java constructs are pretty straightforward, and you can use them readily on the Java side. To illustrate intermixing from Java, let’s write a Scala class:

Intermixing/Car.scala
 
package​ automobiles
 
 
class​ Car(​val​ year: ...

Get Pragmatic Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.