How can I make something like this work:
class Outer {
int some_member;
abstract class InnerBase {
abstract void method();
}
}
class OuterExtendsInner extends Outer.InnerBase {
OuterExtendsInner(Outer o) { o.super(); }
void method() {
// How do I use some_member here?
// Writing Outer.this.some_member -> error about Outer not being an enclosing class
// Writing just some_member -> no-go, either
}
}
The workaround is to have a method in InnerBase that returns Outer.this and call THAT from derived classes, but is there another way?
I primarily want to extend the InnerBase from outside in order to have better code-organization, but I could move all derived classes into Outer.
The problem here is that the synthetic field which links InnerBase to Outer is a private field. Thus, we can only access the outer object from within InnerBase, or if some method or field there provides a reference to the same object.
You could do this in OuterExtendsInner:
class OuterExtendsInner extends Outer.InnerBase {
Outer o;
OuterExtendsInner(Outer o) {
o.super();
this.o = o;
}
void method() {
// now you can reference o.some_member
int x = o.some_member;
}
}
The answer is: you can't, because it would break encapsulation. Only InnerBase can have access to attributes of Outer, not OuterExtendsInner. It is not direct inheritance. InnerBase does not inherit of Outer.
I haven't tried WhiteFang34's answer. It might work, but I'm not clear on it ...
If you really want to define an extension of your inner class elsewhere than in the outer class, the most natural thing would be to define it as an extension of the inner class in another outer extending your outer class as follows:
class Outer {
int some_member;
abstract class InnerBase {
abstract void method();
}
}
class OuterExtendsOuter extends Outer {
class InnerExtendsInner extends Outer.InnerBase {
void method() {
System.out.println(some_member);
}
}
}
I haven't actually run this code either, but it should work.
Update:
Based on the comment thread, I have now compiled and run both my code above and WhiteFang34's code.
Both in fact work, but as noted in the comments by Paŭlo Ebermann, both create two copies of the outer inside the instantiated inner class.
I'm going to upvote Paŭlo's answer, and would advocate just not trying to do this by either tactic, as it's really an abuse of the inner class mechanism.
Just make your extended inner classes live inside the same outer class!
Update 2:
What happens in my code, based on runtime examination using a debugger and on examining the output from javap inspections of the classes, is that both InnerBase and OuterExtendsOuter$InnerExtendsInner have synthetic private final fields named this$0. Because no constructors are explicitly defined, the default constructors are used, and the code snippet
OuterExtendsOuter outer = new OuterExtendsOuter();
Outer.InnerBase inner = outer.new InnerExtendsInner();
causes these two fields to both reference outer.
In other words, Paŭlo's comment is entirely correct.
By further experimentation, the same actually happens if you extend InnerBase in another inner class of Outer, so it has little to do with it being defined in the same outer class or an extension of it, but is in fact an outcome of how non-static inner classes are handled generally.
I suspect this is documented somewhere, but I haven't seen that.
Probably best to mix inheritance and inner classes as little as possible!
Just have a getter method in the InnerBase?
class Outer {
int some_member;
abstract class InnerBase {
abstract void method();
protected int getSome_Member() // This is possible, because Abstract classes can have non-abstract methods.
{
return some_member;
}
}
}
class OuterExtendsInner extends Outer.InnerBase {
OuterExtendsInner(Outer o) { o.super(); }
void method() {
// you can access "some_member" now
int myNumber = getSome_Member();
}
}
Well your problem is that every instance of InnerBase (I know it's abstract) has to have a reference to an Outer object. That is part of the semantics of nested classes. Instantiating OuterExtendsInner would need such a reference.
You can avoid that making InnerBase a static nested class.
The outer class can extend the inner class iff the inner class is compiled to ".class".
Now, every time you compile the outer class it encounters the "extends innerclass" which is
not yet compiled and the compiler will throw a NoClassDefException or ClassNotFoundException.
Isn't it ? So you will never get that inner class compiled. If you can overcome this problem
then you can also extend the inner class :) .
Related
I'm reading about some articles[1][2] online about nested interface in Java, I understand that
interface A {
...
interface B { // this is static by default
...
}
}
But I'm not sure that
class C {
...
interface D { // Is this static by default? Why?
...
}
}
In short, is that "a nested interface is always static" true?
[1] https://beginnersbook.com/2016/03/nested-or-inner-interfaces-in-java/
[2] https://www.programcreek.com/2013/08/inner-interface-in-java/
a non-static inner class is just syntax sugar. A non-static inner class is exactly the same as a standard 'outer' class, with one exception: It has an invisible field of the type of your outer class which is declared final. ALL constructors of your inner class have as first parameter the instance of the outer to which this field must be set.. and then there's a biiig sack of syntax sugar thrown all over this to also hide those.
But that's really how it works, and you can use javap to confirm it. So, given:
public class Outer { public class Inner {} }
versus:
public class Outer {}
class Desugared {
private final Outer outer;
public Desugared(Outer outer) { this.outer = outer; }
}
these things are the same except for syntax:
Outer o = new Outer();
o.new Outer.Inner();
is the same as:
Outer o = new Outer();
new Desugared(o);
etcetera.
Here's the thing: interfaces do not have fields.
Given that they don't, they cannot have this hidden field. And therefore they cannot be 'non-static'. They are therefore implicitly 'static' (they do not have an instance of the outer class implicitly available to them), and you can't change that.
Yes, your "in short" is always true. Quoting directly from the language spec:
A member interface is implicitly static(§9.1.1). It is permitted for the declaration of a member interface to redundantly specify the static modifier.
where "member interface" is defined shortly before:
A member interface is an interface whose declaration is directly enclosed in the body of another class or interface declaration (§8.1.6, §9.1.4).
Classes might be instantiated, interfaces can not be instantiated.
Meaning: when you have an inner class, you might or might not want to create an instance of that inner class with an "outer" object or not.
Assume you have:
class X {
... class Y {
then both someInstanceOfX.new Y() and new X.Y() might make sense.
But when you have an inner interface, the only use of that interface would be with some other class going implements C.D. There is no meaningful way to access C.D that would suggest that D should not be static.
Just as your second link says:
Because an interface can not be instantiated, the inner interface only makes sense if it is static.
Example:
interface Outer {
default String get() {
return "hi";
}
class Inner {
String got() {
return get();
}
}
}
This yields the error
java: non-static method get() cannot be referenced from a static context.
The inner interface/class is always static; unlike with an outer class where it's non-static unless declared static.
This is how things are today and in the upcoming java 8. Is there a fundamental reason for this difference between outer classes and outer interfaces?
Update:
After reading #Radiodef´s comment I changed the inner interface to be an inner class. An outer class can't contain a non-static inner interface so the example was confusing. An inner class is really what I would like anyway.
Update: For reference. This is perfectly legal:
class Outer {
String get() {
return "hei";
}
class Inner {
String got() {
return get();
}
}
}
Maybe I misunderstood your question, but your code snippet is exactly equivalent to
interface Outer {
public default String get() {
return "hi";
}
public static class Inner {
String got() {
return get();
}
}
}
As the JLS Chapter 9.5 (Java 8) states
A member type declaration in an interface is implicitly public and
static. It is permitted to redundantly specify either or both of these
modifiers.
So if you did
Inner innerInstance = new Outer.Inner();
innerInstance.got();
what would get() be invoked on? There is no object of type Outer involved here.
Is there a fundamental reason for this difference between outer
classes and outer interfaces?
This isn't the issue. Your class code is an example of inner classes, ie. non static nested class. The interface code is an example of a static nested class. You are comparing two different things.
The equivalent example with a static nested class within an enclosing class would be
class Outer {
String get() {
return "hei";
}
public static class Inner {
String got() {
return get(); // won't compile
}
}
}
where again it doesn't make sense for get() to work since there is no corresponding (enclosing) instance to invoke it on.
If the question, as #Radiodef put it, is
why must the class be implicitly static beyond that this is the
existing spec?
then my answer is the following:
An interface, by definition, is
A point at which independent systems or diverse groups interact
An interface does not have state and it does not have behavior. It simply describes behavior. Interface members are implicitly static because an interface does not have state.
I will provide a way for you to remember it.
For a static member, it's not bound to an object of the declaring class/interface; for a non-static member, it has to bound to an object of the declaring class.
All interface's members are implicitly static, except default ones.
So, in your Exemple: static class Inner is not bound to an object of interface Outer(Since there is no meaning of object of interface), so it can't call the member of Outer.
For your legal one in the Update: class Inner is bound to an object of class Outer, so when you create an object of Inner, you also create an anonymous object of class Outer, so when you call get() in Inner::got(), the get() is called upon that anonymous object.
Hope this help.
How can I load a class that is already on the class path, instantiate it, and also instantiate any inner classes defined within it?
EG:
public class TestClass {
public class InnerClass { }
}
Inner classes cannot exist outside the parent class. You need to construct the parent class first. Without reflection this would look like:
InnerClass innerClass = new TestClass().new InnerClass();
In reflection, you need to pass the parent class in during construction of the inner class.
Object testClass = Class.forName("com.example.TestClass").newInstance();
for (Class<?> cls : testClass.getClass().getDeclaredClasses()) {
// You would like to exclude static nested classes
// since they require another approach.
if (!Modifier.isStatic(cls.getModifiers())) {
Object innerClass = cls
.getDeclaredConstructor(new Class[] { testClass.getClass() })
.newInstance(new Object[] { testClass });
}
}
As a side note, given that your primary question has been answered - often people will declare inner classes as in your example above, without giving a thought to whether they can be static inner classes instead.
In my experience, the vast majority of (non-anonymous) inner classes can be static, as they don't need to access their parent class' instance members. Declaring the inner class as static in this case is both more efficient (as the runtime doesn't need to define a new class for each parent instance), less confusing (since new TestClass().new InnerClass().getClass() != new TestClass().new InnerClass().getClass()) and easier to instantiate if you don't have an appropriate instance of TestClass handy.
So if this applies to you, you could (and arguably should) declare you inner class thusly:
public class TestClass {
public static class InnerClass { }
}
and then you can simply instantiate it as new TestClass.InnerClass().
(If you need to access member fields from within InnerClass, then just ignore all this of course!)
Class.forName("your classname").newInstance().
The inner classes will be instantiated only if the constructor instantiates them.
I'm having a hard time wrapping my head around non-static nested classes in Java. Consider the following example, which prints "Inner" and then "Child".
class Outer {
class Inner {
Inner() { System.out.println("Inner"); }
}
}
public class Child extends Outer.Inner {
Child(Outer o) {
o.super();
System.out.println("Child");
}
public static void main(String args[]) {
new Child(new Outer());
}
}
I understand that instances of Inner always have to be associated with an Outer instance, and that that applies to Child too since it extends Inner. My question is what the o.super() syntax means - why does it call the Inner constructor?
I've only seen a plain super(args) used to call the superclass constructor and super.method() to call the superclass version of an overridden method, but never something of the form instance.super().
It's called a "qualified superclass constructor invocation".
Citing from here:
Explicit constructor invocation statements can be divided into two kinds:
Alternate constructor invocations begin with the keyword this (possibly prefaced with explicit type arguments). They are used to invoke an alternate constructor of the same class.
Superclass constructor invocations begin with either the keyword super (possibly prefaced with explicit type arguments) or a Primary expression. They are used to invoke a constructor of the direct superclass. Superclass constructor invocations may be further subdivided:
Unqualified superclass constructor invocations begin with the keyword super (possibly prefaced with explicit type arguments).
Qualified superclass constructor invocations begin with a Primary expression . They allow a subclass constructor to explicitly specify the newly created object's immediately enclosing instance with respect to the direct superclass (§8.1.3). This may be necessary when the superclass is an inner class.
Inner Classes (non-static child classes) are essentially Nested Classes (static child classes) with implicit links back to their parent objects. Here is your above code, written instead using a static nested class:
class Outer {
static class Inner {
final Outer outer;
Inner(Outer outer) {
this.outer = outer;
System.out.println("Inner");
}
}
}
public class Child extends Outer.Inner {
Child(Outer o) {
super(o); // o.super();
System.out.println("Child");
}
public static void main(String args[]) {
new Child(new Outer());
}
}
Looking at this, you should be able to understand what o.super() was doing.
Why does o.super() in Child ends up invoking Outer.Inner constructor? It's simple: because Child extends Outer.Inner, and constructor calls are always chained up the hierarchy.
Here's a slight expansion to your snippet to illustrate:
class Outer {
Outer() {
System.out.println("Outer");
}
void outerMethod() { }
class Inner {
Inner() {
System.out.println("OuterInner");
outerMethod();
}
String wealth;
}
}
class OuterChild extends Outer {
OuterChild() {
System.out.println("OuterChild");
}
}
public class OuterInnerChild extends Outer.Inner {
OuterInnerChild(Outer o) {
o.super();
System.out.println("OuterInnerChild");
this.wealth = "ONE MILLION DOLLAR!!!";
}
public static void main(String args[]) {
System.out.println(new OuterInnerChild(new Outer()).wealth);
new OuterChild();
}
}
This prints:
Outer
OuterInner
OuterInnerChild
ONE MILLION DOLLAR!!!
Outer
OuterChild
Some key observations:
Because OuterInnerChild extends Outer.Inner, it inherits wealth, just like normal subclass semantics
And just like normal subclass semantics, the constructor of OuterInnerChild chains to the constructor of Outer.Inner
Because OuterChild extends Outer, its constructor chains, even when not invoked explicitly
Whether implicitly or explicitly, the constructor chains up the hierarchy
But why does the compiler demand that OuterInnerChild constructor takes an Outer o, and that o.super() is invoked?
Now that is specific to inner class semantics: it's done to ensure that all instances of OuterInnerChild has an enclosing Outer instance for Outer.Inner, the super class of OuterInnerChild. Otherwise, the constructor of Outer.Inner would not have an enclosing instance of Outer to invoke outerMethod() on.
Conceptually, a non-static inner class “belongs” to a particular object. It's sorta like each one gets its own version of the class, much like a non-static field or method belongs to a particular object.
So that's why we have funny syntax like instance.new Inner() and instance.super() — for contexts where the answer to the question “but whose Inner?” isn't immediately obvious. (In a non-static method of the outer class, you can just say new Inner(), and as usual that's short for this.new Inner().)
Always not to forget basic principles, in process of calling a sub class constructor it's always the parent class is instantiated first irrespective of inner/outer classes. In your scenario, as you are extending inner class and your inner class is a member of parent class which needs to be instantiated then followed by calling the actual inner class constructor.
I have seen Java code that says something like:
SomeClass.this.someMethod(someArg);
Blah(AnotherClass.class);
Blah(YAClass.this);
What do "this" and "class" mean here? I am used to them as keywords to refer to the current object and to define a class, but this is different. My Java book and online searches have not yielded any explanation.
SomeClass.this/YAClass.this - the this reference of an inner class' enclosing SomeClass/YAClass class.
class SomeClass {
private InnerClass {
public void foo() {
SomeClass outerThis = SomeClass.this;
[...]
}
}
}
(You need to be very careful which this you get particularly when dealing with operations that could be applied to any Object reference. A common case is syncronising on this in an inner class, when the code should be synchronising on the outer instance (a better approach in this case is to use an explicit lock object).)
AnotherClass.class - the java.lang.Class object for the AnotherClass class. Prior to Java 1.5 this was implemented using Class.forName (initialising the class); from 1.5 the ldc bytecode has been extended for direct support.
Class<AnotherClass> clazz = AnotherClass.class;
Both were introduced in Java 1.1.
A quick example for inner class, to complete the other answers:
class SomeClass {
public void someMethod() {
System.out.println("Hello, I have someMethod");
}
public void otherMethod() {
addListener(new MyListener() {
public void someMethod () {
System.out.println("I too, have someMethod");
}
public void listen() {
// I will call someMethod of SomeClass:
SomeClass.this.someMethod();
}
});
}
}
The .class syntax refers to a particular instance of the Class class.
The .this syntax is usually used from within inner classes to refer to the enclosing instance of the top-level class. If you used just this from within an inner class it would refer to the instance of the inner class and not the enclosing class.
.class refers to the Class object corresponding to your instance's class. Java keeps one Class around in memory per referenced type.
Here is the Javadoc for Class: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html
The only time I have seen SomeClass.this used is when you are dealing with nested classes, and need to refer to the instance of the outer class from the inner class. See here for an example: http://juixe.com/techknow/index.php/2009/04/07/java-nested-inner-class-this/