Suppose I have an inner class B that I will create an instance of inside one of the member functions of outer class, is there a way that I can do it without creating a instance of outer class?
Thanks a lot.
class A {
class B {
}
public void function() {
// create an instance of B, normally have to create a
// instance of A to be bond to by b
B b = new B();
}
}
The only way you do this is by declaring B static:
class A {
static class B {
This means that B does not have an instance of A associated with it, and can be instantiated in contexts where no outer instance of available.
That said, your current code compiles fine as-is (since function() is non-static, there is an instance of A available in this context).
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