I don't really blog anymore. Click here to go to my main website.

muhuk's blog

Nature, to Be Commanded, Must Be Obeyed

May 24, 2016

How to Call Scala From Java: Using Scala Classes

This is the first part of a guide to integrate Scala code from Java. It can be considered as the dual of Scala Tutorial for Java Programmers, which guides Java programmers write Scala code. My aim is help programmers writing Java code that deals with Scala objects (as in JVM objects, not necessarily Scala object’s).

This is the Scala class used throughout the examples in this post:

final class ScalaClass(aString: String, val anInteger: Int) {
    def this(aBool: Boolean) {
        this("defaultString", -1)

    val theString = "theString"

    var someString = "some"

    def plus(x: Int, y: Int): Int = x + y

We will assume the following static imports for Java code in this post:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Instantiating a Class

Instantialization of Scala classes work exactly as in Java.

Let’s start with initializing our class using the primary constructor:

String aString = "aString";
int anInteger = 42;
assertTrue(new ScalaClass(aString, anInteger) instanceof ScalaClass);

In case you don’t know, primary constructor is ScalaClass(aString: String, val anInteger: Int) and secondary constructors are defined as one or more this functions[1].

Instantialization using secondary constructors is also trivial:

boolean aBool = false;
assertTrue(new ScalaClass(aBool) instanceof ScalaClass);

Calling Methods

Calling methods on Scala instances work exactly as in Java.

ScalaClass scalaClass = new ScalaClass(true);
assertEquals(2, scalaClass.plus(1, 1));

Note that Scala classes cannot have static methods. The convention is to put methods of static nature on companion objects. Actually there is a bit more about not having a static keyword. If you are not familiar with immutability, functions as means of combination and functional programming in general Structure and Interpretation of Computer Programs is great primer. If your method is not going to access any instance attributes why make it static could be offered as a short explanation. In any case, Scala object’s will be covered in a future post.

Accessing Attributes

Accessing Scala attributes is done via auto-generated getters/setters.

Our ScalaClass has three attributes:

val anInteger: Int

val theString = "theString"

var someString = "some"

While anInteger is an attribute aString is just a constructor parameter. The difference between aString and a Java constructor parameter is; it is accessible from within the class definition. In other words aString is a private immutable attribute (private val).

Since anInteger and theString are val’s, they can only be read. Reading an (accessible) attribute is done by calling the zero-arity function with the same name:

int anInteger = 42;
ScalaClass scalaClass = new ScalaClass(null, anInteger);
assertEquals("theString", scalaClass.theString());
assertEquals(anInteger, scalaClass.anInteger());

A mutable attribute is read the same way. Writing (setting) can be done by calling {attribute-name}_$eq method with the new value:

ScalaClass scalaClass = new ScalaClass(null, 0);
assertEquals("some", scalaClass.someString());
String another = "another";
assertEquals(another, scalaClass.someString());

This concludes class usage. Next post will be about inheritance. You can find the code here. Let me know if I have missed anything important, or if you have questions.

[1]Official documentation doesn’t seem to mention secondary constructors at all.

If you have any questions, suggestions or corrections feel free to drop me a line.