This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Frequently Asked Questions - Java Interoperability


The standard Scala backend is a Java VM. Scala classes are Java classes, and vice versa. You can call the methods of either language from methods in the other one. You can extend Java classes in Scala, and vice versa. The main limitation is that some Scala features do not have equivalents in Java, for example traits.

In Scala Version 2.7.0, Java generics were made visible to Scala programs.  In Scala Version 2.7.2, Scala generics were made visible to Java programs.  However, higher-kinded type parameters are not usable from Java programs. 

It is not documented at the moment. In general, though, Scala is designed to be Java compatible, and its translation attempts to map like constructs to like. Scala classes become same-named Java classes, etc. To know for sure, however, you must use tools like javap.

Accessing Java classes from Scala code is no problem at all. Using a Scala class from Java can get tricky, in particular if your Scala class uses advanced features like generics, polymorphic methods, or abstract types. Since Java doesn't have such language features, you have to know something about the encoding scheme of Scala classes. Otherwise, there should be no problems.

You do not have to convert Java data structures at all for using them in Scala. You can use them "as is". For instance, Scala classes can subclass Java classes, you can instantiate Java classes in Scala, you can access methods, fields (even if they are static), etc.

Not at the moment, although there is a project underway to provide this functionality.

This is a known limitation of Scala: there is no notion of 'static' members in Scala.  Instead, Scala treats static members of class Y as members of the singleton object Y (the companion object of class Y). When inheriting from this class, one can access only protected members of class Y but cannot access protected members of object Y.

There's no way Scala can simulate static protected without impairing the integrity of Scala's object model in a fundamental way, so this is not going to change. To work around this limitation, one has to create an implementation of the enclosing class with Java code which encapsulates all accesses to the protected static inner class.

See ticket #1806 for more information and a concrete example of the limitation and its workaround.

 

 

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland