Why can't I do this / is there a workaround for this:
package myPackage; public class A { public class B { } }
package myPackage; import myPackage.AB; public class C extends B { }
package myPackage; public class Main { public static void main(String[] args) { A myA = new A(); C myC = myA.new C(); } }
Two compilation errors:
In public class C extends B , No enclosing instance of type A is available due to some intermediate constructor invocation
On C myC = myA.new C(); , AC cannot be resolved to a type
Honestly, I think a conceptual idea sounds like this: I want to subclass B so that when I create B for A, I have the opportunity to make it functionality in B or functionality in C.
Four workarounds / solutions that I don't need and why I don't want them:
"Solution: put C inside A." I do not want this because if I cannot change the code for A.java (are there applications that have this limitation)? What if A is part of another API? Then I need to create a new file for C, as I did here.
"Solution: Put C inside a class D that extends A." I do not want this, because then C is limited only to an instance of instances of type D. I want to create a class that extends B, which can be created in all instances of type A (there are applications that need this). Therefore, I need C to not be closed by another class, as I did here.
(Added as an editing question - see JoshuaTaylor answer for sample code) "Solution: make statics B". I don't want this because if the functionality in B needs to access its encompassing instance of A (are there applications that need it)? Therefore, I need B to not be static, as I did here. (Editing 2nd question: you can make B static and create your constructor in your enclosing instance, storing it in a protected variable to access your children, but this is less elegant than the accepted RealSkeptic answer)
Deleted. See edit below.
So, if your answer tells me that I am doing one of the above, this is not the answer to this question, although it may be useful to other people.
If your answer: βThis is just a flaw in the Java language, you simply cannot fulfill this conceptual idea,β is a good answer, and you should post it. Just a warning though: I will hold on to marking your answer, as is the case in case you are mistaken. If this is your answer, I would really appreciate it if you have an explanation why this language restriction exists (as the name of this question).
Thanks for any help.
EDIT: JoshuaTaylor's answer raises a valid option: you can anonymously and avoid writing a constructor, as in the accepted RealSkeptic answer. I initially abandoned this idea because it does not allow you to access an instance of C enclosed through A through "A.this". However, since then I have learned that C does not have a spanning instance of A unless it is defined in the definition of A as a nested class. Therefore, note that none of the solutions below allows you to access an instance of environment A that encloses the ancestor of B from B by writing "A.this" in method C. Classes can only use ".this" to access types that they are nested in particular. However, if B has functionality that accesses an enclosing instance of A, either an anonymous class using the JoshuaTaylor method or any other class using the RealSkeptic method is required.
java nested-class
snickers10m
source share