Supposing I have the following classes:
abstract class A {
static abstract class _Foo {}
}
class B extends A {
static void doSomething() {
System.out.println(C.saySomething());
}
static _Foo getInner() {
return new C._Foo();
}
static abstract class _Foo extends A._Foo {}
}
final class C extends D {
static String saySomething() {
return "Something";
}
}
abstract class D {
static class _Foo extends B._Foo {
public int value() {
return 42;
}
}
}
To provide some context:
C
and D
are generated at compile timeA
as well as C
are never instantiated; they simply provide some behaviour for class B
B
is the only one that is actually used.D
is unknown until after compile time, which is why we are only using C
in B
.This is similar to what one might expect when working with google autovalue
My question is with regards to the getInner
function in B
:
_Foo
will be instantiated at the line return new C._Foo();
? The _Foo
in D
or the one in A
?The last question is just as an FYI, I'm mostly interested in the first two.
Thanks for your help.
To answer your three questions...
_Foo
in D
will be returned from new C._Foo()
._Foo
s are "members" of the classes they are declared in, so they are inherited much like variables. (In this case, like static variables.) [Spec] (https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.2). (This is the best documentation I could muster up.)C
. Since C
extends D
, C
will inherit whatever D
has (i.e. D._Foo
). If D
didn't declare a class called _Foo
, then D
would inherit B
's _Foo
, and then C
would inherit that.Hope this helps. :)