O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required