S
srikanthyellanki
why static methods of outer class cannot create the object of non-
static inner class?
static inner class?
why static methods of outer class cannot create the object of non-
static inner class?
Chris said:Because they don't have access to an instance for the inner
object to point to?
why static methods of outer class cannot create the object of non-
static inner class?
They can, but you have to pass an instance of the class to every constructor
used in that way. The syntax for doing that is wierd (and pointlessly so, IMO)
but is well-defined: E.g.
================
class Outer
{
class Inner
{
Inner(int param)
{
}
}
static void
test()
{
Outer instance = new Outer();
Inner inner = instance.new Inner(42);
}}
================
-- chris
Daniel said:The syntax makes sense if you think about the order symbols are
resolved. Eventually, the compiler will look for an implied "this.",
which would lead to "this.new Inner(42)";
Okay, maybe its weird. but would you rather use "new
instance.Inner(32)"?
Chris Uppal said:Hmm... Yes, I see your point.
I'd rather the code looked as if it were doing what it is /acutally/
doing. So
the inner class's constructor took an /explicit/ instance of the enclosing
class as its first parameter.
Hmm... Yes, I see your point.
I'd rather the code looked as if it were doing what it is /acutally/ doing. So
the inner class's constructor took an /explicit/ instance of the enclosing
class as its first parameter.
-- chris
[...]I'd rather the code looked as if it were doing what it is /acutally/
doing. So the inner class's constructor took an /explicit/ instance of
the enclosing class as its first parameter.
So, you'd prefer this:
class Outer {
class Inner {
}
Inner getInner() {
return new Inner(this);
}
}
Hmm, thats broken in more ways than one...
However, if you have two coupled
classes, and one is exclusively created inside the other, why not take
advantage of the implicit Outer.this.
Daniel Pitts wrote:
[me:]
[...]I'd rather the code looked as if it were doing what it is /acutally/
doing. So the inner class's constructor took an /explicit/ instance of
the enclosing class as its first parameter.
So, you'd prefer this:
class Outer {
class Inner {
}Inner getInner() {
return new Inner(this);
}
}Hmm, thats broken in more ways than one...
I don't see how it can be "broken" when that's what is actually happening
today.
However, if you have two coupled
classes, and one is exclusively created inside the other, why not take
advantage of the implicit Outer.this.
I have no objection to the implicit reference to the outer object. What
doesn't sit well with me is the implicit /establishment/ of that reference. It
doesn't seem that the "pretty" syntax (which tries to hide the connection) is
any prettier than the "raw" alternative -- there's not a lot of point in
syntactic sugar that isn't even sweet...
-- chris
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.