You can also go back to the FAQ page.
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).
It is worth mentioning that, in the Eclipse plugin, classes in the default package cannot be accesed from Scala.
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.
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.
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) ...
While someone writes up good instructions, here are a couple of threads in the Scala mailing list about the topic:
Seriously, someone ought to put this information in the Eclipse plugin page, right next to the “hello world” example. (Emacs users can handle it on their own.)