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?
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.
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 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.
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.
Hmm, I could just read the Java Interoperability FAQ:
http://www.scala-lang.org/faq/4#4n1381
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With