Using Java classes from Scala is pretty straightforward. If the Java class you’d like to use is part of the standard JDK, then simply use it. You’ll have to import the class if it’s not part of the java.lang package. Let’s use the java.util.Currency class from the JDK:
Intermixing/UseJDKClass.scala | |
| import java.util.Currency |
| |
| val currencies = Currency.getAvailableCurrencies |
| println(s"${currencies.size} currencies are available.") |
No extra steps of compilation are needed. Scala scripts can directly use Java classes. To run this script, type the following:
| scala UseJDKClass.scala |
The output from running the script is
| 220 currencies are available. |
If the Java class you’d like to use is not from the JDK but is your own or from a third party, make sure to specify to scala the classpath of where the bytecode is located. Suppose we have the following Java files:
Intermixing/java/InvestmentType.java | |
| //Java code |
| package investments; |
| |
| public enum InvestmentType { |
| BOND, STOCK, REAL_ESTATE, COMMODITIES, COLLECTIBLES, MUTUAL_FUNDS |
| } |
Intermixing/java/Investment.java | |
| //Java code |
| package investments; |
| |
| public class Investment { |
| private String investmentName; |
| private InvestmentType investmentType; |
| |
| public Investment(String name, InvestmentType type) { |
| investmentName = name; |
| investmentType = type; |
| } |
| public int yield() { return 0; } |
| } |
We can use these Java classes from Scala just like we use any Scala class. Here’s an example of creating an instance of Investment in Scala:
Intermixing/UseInvestment.scala | |
| import investments._ |
| |
| object UseInvestment extends App { |
| val investment = new Investment("XYZ Corporation", InvestmentType.STOCK) |
| println(investment.getClass) |
| } |
Let’s compile the Java code, place the bytecode in a directory named classes/investments, and then use that to compile the Scala code. Here are the commands to compile and run:
| mkdir -p classes |
| javac -d classes java/InvestmentType.java java/Investment.java |
| scalac -classpath classes UseInvestment.scala |
| scala -classpath classes:. UseInvestment |
Alternately, once we compile the source files, as a last step, instead of using the scala tool, we can also run it using the java tool:
| java -classpath $SCALA_HOME/lib/scala-library.jar:classes:. UseInvestment |
Make sure to set up the environment variable SCALA_HOME to point to the location where Scala is installed on your system. Also, if you’re on Windows, replace the environment variable reference $SCALA_HOME with %SCALA_HOME%.
That worked seamlessly, but there’s a catch. Use caution with the yield method of the Investment class. If your Java code has methods or field names—like trait, yield, and so on—that conflict with Scala keywords, the Scala compiler will choke up when you call them. For example, the following code won’t work:
| val theYield1 = investment.yield //ERROR |
| val theYield2 = investment.yield() //ERROR |
Fortunately, Scala offers a solution to resolve keyword conflicts—you can place the affected variables/methods in a backtick. To get the previous two calls to work, modify the code as follows:
| val investment = new Investment("XYZ Corporation", InvestmentType.STOCK) |
| val theYield1 = investment.`yield` |
| val theYield2 = investment.`yield`() |
On seeing the backtick, Scala resolves the field or method as a member of the target instance instead of a keyword in the language.
3.138.170.174