0

The JLS says:

The constructor of a non-private inner member class implicitly declares, as the first formal parameter, a variable representing the immediately enclosing instance of the class.


Ok, if we write the following:

class A:

package org.gradle; public class A extends B.Inner{ public A(B b){ b.super(); //OK, invoke B.inner(B) } } 

class B:

package org.gradle; public class B{ public class Inner{ } } 

As said here, b.super() actually invoke B.Inner(B).


But if we write

class B:

package org.gradle; public class B { class Inner{ public Inner(B b){ System.out.println("Inner(B)"); } } } 

class A:

package org.gradle; public class A extends B.Inner{ public A(B b) { b.super(); //The constructor B.Inner() is undefined } } 

So, in the latter example b.super() tries to invoke B.Inner() instead. Why is that so difference?

10
  • You need to call b.super(b). The "invisible" first parameter isn't shown in the error message, because it's "invisible". Commented Nov 8, 2014 at 7:28
  • @immibis I don't exactly understand what you say. I know how to fix the second example. I want to realise it. Commented Nov 8, 2014 at 7:29
  • If you wrote b.super(5); in the first example, would you it say "B.Inner(B, int) is undefined" or "B.Inner(int) is undefined"? Commented Nov 8, 2014 at 7:30
  • Ok, why in the second example b.super() doesn't invoke B.Inner(B), but is in the first does? Commented Nov 8, 2014 at 7:32
  • It does try to invoke B.Inner(B). Commented Nov 8, 2014 at 7:33

1 Answer 1

3

It does try to invoke B.Inner(B) in your second example.

It can't find it because there's only a B.Inner(B, B). If your constructor is B.Inner() then it gets changed to Inner(B)... and if your constructor is B.Inner(B) then it gets changed to B.Inner(B, B).

Note that the hidden parameter is effectively an implementation detail, and unless you're studying how the Java compiler works, you don't need to know it exists.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.