Using Scala Classes from Scala

Before diving into mixing Java and Scala, let’s first look at using Scala classes from Scala. If you’ve created Scala classes in separate files, compile them to bytecode using the Scala compiler scalac. Then drop them into a JAR file using the jar tool. In the next example, we compile two classes, Person and Dog, and then create a JAR from the resulting class files. Let’s take a look at the classes first:

Intermixing/Person.scala
 
class​ Person(​val​ firstName: ​String​, ​val​ lastName: ​String​) {
 
override​ ​def​ toString : ​String​ = firstName + ​" "​ + lastName
 
}
Intermixing/Dog.scala
 
class​ Dog(​val​ name: ​String​) {
 
override​ ​def​ toString = name
 
}

Here are the commands to compile and create the ...

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.