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

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