====== The Scala language ======
- [[faqs:interoperability#Can I use Java classes from within my Scala code?]]
- [[faqs:interoperability#Can I use Scala classes from within my Java code?]]
- [[faqs:interoperability#I cannot access static members of a Java class via a subclass]]
- [[faqs:interoperability#How to deal with Java class members having a name that is a reseverd word in Scala?]]
- [[faqs:interoperability#How do I organize mixed Java and Scala projects?]]
You can also [[faqs:start|go back to the FAQ page]].
===== Can I use Java classes from within my Scala code? =====
Accessing Java classes from Scala code is no problem at all. Simply treat the Java class as if it was a Scala class.
Java 1.5 generics are a bit tricky to deal with as they don't map exactly to Scala generics (Java's wild-cards are the trouble spot). Scala 2 has experimental support for Java generics since version 2.1.0 (SVN revision 5871).
===== Can I use Scala classes from within my Java code? =====
There is always a way (after all, Scala runs on the JVM!), but it is sometimes tricky, especially when using advanced Scala features without a close Java analog. Use javap to see how any of your individual classes have been encoded into Java. See [[Scala to Java Mapping]] for more general notes on the mapping.
If you have a fancy Scala class that is not encoding nicely into Java, one trick you can use is to make a Scala singleton object with methods that provide some limited view over your Scala code. Methods of singleton objects become static methods in a Java wrapper class with the same name as the Scala singleton object.
For Scala objects (singletons), it seems like it sometimes helps if you use the $ after their name in the Java code. So if you have a Scala object
:object Foo{}
then sometimes you can refer to it as
:Foo$ my_Foo = new Foo$()
in Java. Unfortunately this seems to be poorly documented.
===== I cannot access static members of a Java class via a subclass =====
Right. In Java you can write
public class A {
public static int a = 1;
public int n;
}
public class B extends A {
public static int b = 2;
public int m;
}
class Test {
public static void main(String[] args) {
System.out.println(B.b); // ok
System.out.println(B.a); // ok
}
}
In Scala, however, this can not be done. You have to refer to static members via the name of the class in which they were defined. Scala doesn't have the notion of static members. Instead, it deals with singleton objects. For every Java class the Scala compiler creates two symbols. One is the symbol of the class with its instance members. The other is the symbol of a singleton object, with the same name as the class, which contains the static members. Effectively, what the Scala compiler sees is
object A { val a: Int; }
class A { val n: Int; }
object B { val b: Int; }
class B extends A { val m: Int; }
Now, objects can not be subclassed. There is no way to relate such object to the corresponding object of its Java superclass. Therefore, there's no way to make members of the superclass' object appear as members of the subclass' object.
===== How to deal with Java class members having a name that is a reserved word in Scala? =====
For example: the class //org.eclipse.swt.widgets.Event// has a member //type//. To avoid compiler errors when using this member in scala, just put between back-quotes: if (event.`type` == SWT.MouseMove) ...
===== How do I organize mixed Java and Scala projects? =====
As of 2.7.4 scalac directly supports mixed Scala and Java projects.