Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static nested class visibility issue with Scala / Java interop

Suppose I have the following Java file in a library:

package test;
public abstract class AbstractFoo {
   protected static class FooHelper {
      public FooHelper() {}
   }
}

I would like to extend it from Scala:

package test2
import test.AbstractFoo
class Foo extends AbstractFoo {
  new AbstractFoo.FooHelper()
}

I get an error, "class FooHelper cannot be accessed in object test.AbstractFoo". (I'm using a Scala 2.8 nightly). The following Java compiles correctly:

package test2;
import test.AbstractFoo;
public class Foo2 extends AbstractFoo {
    { new FooHelper(); }
}

The Scala version also compiles if it's placed in the test package. Is there another way to get it to compile?

like image 289
Matt R Avatar asked May 08 '10 13:05

Matt R


People also ask

How do I access static nested classes?

They are accessed using the enclosing class name. For example, to create an object for the static nested class, use this syntax: OuterClass. StaticNestedClass nestedObject = new OuterClass.

What are restrictions on static nested classes?

A static nested class cannot access non-static members of its outer class because it does not have an implicit reference to an outer object. 4. Non-static members of a normal inner class can access the static members of any static nested class within the same outer class.

Which one of the statement given in options is true about the static nested classes?

Which statement is true about a static nested class? You must have a reference to an instance of the enclosing class in order to instantiate it.

Does nested class increase encapsulation?

It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private . By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.


1 Answers

Hmm, I could just read the Java Interoperability FAQ:

http://www.scala-lang.org/faq/4#4n1381

like image 182
Matt R Avatar answered Oct 08 '22 22:10

Matt R