- About Scala
- In the Enterprise
- Scala Community
- Language Research
- In the Press
- The Scala Team
- Scala's Prehistory
- Contact Us
- Learning Scala
- Tour of Scala
- Scala API
- Setup & Getting Started
- Programming Guides
- Other Guides
- Code Examples
- Scala Developers
Frequently Asked Questions - Java Interoperability
- What does it mean that Scala is compatible with Java?
- Are Java 1.5 generics available in Scala?
- Where can I read about the translation from Scala to Java?
- Can I use existing Java code from Scala?
- Do I need to convert Java data structures to Scala, and vice versa?
- Can I compile Scala code into Java source (for example, so I can use GWT)?
- Why does accessing a protected static inner class X of a superclass Y fail with "error: class X cannot be accessed in object Y"?
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
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.
Why does accessing a protected static inner class X of a superclass Y fail with "error: class X cannot be accessed in object Y"?
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.