From: Scott A. Hightower on 27 Apr 2010 21:16 Given a reference to an inner class instance, is there a direct way to refer to its enclosing class instance? I can do it indirectly by returning a qualified "this" from a method of the inner class: class Outer { int i; /* ... */ class Inner { Outer getMom() { return Outer.this; } } } class X { void bump(Outer.Inner y) { Outer z = y.getMom(); // Outer z = what without getMom()? z.i++; } }
From: Mike Schilling on 27 Apr 2010 21:42 Scott A. Hightower wrote: > Given a reference to an inner class instance, is there a direct way > to refer to its enclosing class instance? > > I can do it indirectly by returning a qualified "this" from a method > of the inner class: > > class Outer { > int i; > /* ... */ > class Inner { > Outer getMom() { return Outer.this; } > } > } > > class X { > void bump(Outer.Inner y) { > Outer z = y.getMom(); > // Outer z = what without getMom()? > z.i++; > } > } I'm fairly sure the answer is "no". In fact, I don't think that, without using reflection, it would be possible for X to determine whether an Inner even has an enclosing instance (that is, whether it's an inner class or a nested class.)
From: Scott A. Hightower on 28 Apr 2010 00:27 "Mike Schilling" <mscottschilling(a)hotmail.com> wrote in message news:hr83q3$228$1(a)news.eternal-september.org... > Scott A. Hightower wrote: >> Given a reference to an inner class instance, is there a direct way >> to refer to its enclosing class instance? >> >> I can do it indirectly by returning a qualified "this" from a method >> of the inner class: >> >> class Outer { >> int i; >> /* ... */ >> class Inner { >> Outer getMom() { return Outer.this; } >> } >> } >> >> class X { >> void bump(Outer.Inner y) { >> Outer z = y.getMom(); >> // Outer z = what without getMom()? >> z.i++; >> } >> } > > I'm fairly sure the answer is "no". In fact, I don't think that, without > using reflection, it would be possible for X to determine whether an Inner > even has an enclosing instance (that is, whether it's an inner class or a > nested class.) > (My apologies to Mike - I hit the wrong button and sent this directly to him.) In this particular case, the programmer knows that Inner has an enclosing instance, knows the class name for that instance and knows that the fields of interest are accessible. I was just wondering. The getMom() method does the trick. Just seemed a klunky way to get it. But darned if I can think of what kind of syntax the expression would have. Probably not much need for it, and probably a sign that the design needs rethinking. I know this one certainly does, just hope there's time to refactor before launch. Anyway, thanks for responding
From: Mike Schilling on 28 Apr 2010 01:00 Scott A. Hightower wrote: > "Mike Schilling" <mscottschilling(a)hotmail.com> wrote in message > news:hr83q3$228$1(a)news.eternal-september.org... >> Scott A. Hightower wrote: >>> Given a reference to an inner class instance, is there a direct way >>> to refer to its enclosing class instance? >>> >>> I can do it indirectly by returning a qualified "this" from a method >>> of the inner class: >>> >>> class Outer { >>> int i; >>> /* ... */ >>> class Inner { >>> Outer getMom() { return Outer.this; } >>> } >>> } >>> >>> class X { >>> void bump(Outer.Inner y) { >>> Outer z = y.getMom(); >>> // Outer z = what without getMom()? >>> z.i++; >>> } >>> } >> >> I'm fairly sure the answer is "no". In fact, I don't think that, >> without using reflection, it would be possible for X to determine >> whether an Inner even has an enclosing instance (that is, whether >> it's an inner class or a nested class.) >> > > (My apologies to Mike - I hit the wrong button and sent this directly > to him.) > > In this particular case, the programmer knows that Inner has an > enclosing instance, knows the class name for that instance and knows > that the fields of interest are accessible. > > I was just wondering. The getMom() method does the trick. Just > seemed a klunky way to get it. > > But darned if I can think of what kind of syntax the expression would > have. Yeah, that was my point. If there were syntax for such a thing, it should compile correctly for inner classes but not nested classes, and I don't know of any such animal.
From: Daniel Pitts on 28 Apr 2010 18:32 On 4/27/2010 10:00 PM, Mike Schilling wrote: > Scott A. Hightower wrote: >> "Mike Schilling"<mscottschilling(a)hotmail.com> wrote in message >> news:hr83q3$228$1(a)news.eternal-september.org... >>> Scott A. Hightower wrote: >>>> Given a reference to an inner class instance, is there a direct way >>>> to refer to its enclosing class instance? >>>> >>>> I can do it indirectly by returning a qualified "this" from a method >>>> of the inner class: >>>> >>>> class Outer { >>>> int i; >>>> /* ... */ >>>> class Inner { >>>> Outer getMom() { return Outer.this; } >>>> } >>>> } >>>> >>>> class X { >>>> void bump(Outer.Inner y) { >>>> Outer z = y.getMom(); >>>> // Outer z = what without getMom()? >>>> z.i++; >>>> } >>>> } >>> >>> I'm fairly sure the answer is "no". In fact, I don't think that, >>> without using reflection, it would be possible for X to determine >>> whether an Inner even has an enclosing instance (that is, whether >>> it's an inner class or a nested class.) >>> >> >> (My apologies to Mike - I hit the wrong button and sent this directly >> to him.) >> >> In this particular case, the programmer knows that Inner has an >> enclosing instance, knows the class name for that instance and knows >> that the fields of interest are accessible. >> >> I was just wondering. The getMom() method does the trick. Just >> seemed a klunky way to get it. >> >> But darned if I can think of what kind of syntax the expression would >> have. > > Yeah, that was my point. If there were syntax for such a thing, it should > compile correctly for inner classes but not nested classes, and I don't know > of any such animal. Strangely, I know there is syntax for the reverse proposition. Creating an Inner instance from an Outer: class X { void bump(Outer z) { z.i++; Outer.Inner in = z.new Inner(); } } I could find no way to access z from in though. -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
|
Next
|
Last
Pages: 1 2 Prev: Reading the BAM or Directory Next: Handling extremely large input files |