• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner class data access

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is code with a method-local inner class. The outer class, the method in which the inner class is defined and the inner class itself have a final attribute called string. How can you access the string defined in the method in this case?


This prints
default string : Private attribute of inner class
this.string : Private attribute of inner class
InnerTest.this.string : private outer string

as expected. So how do u explicity reference the other string to print
"Method defined string"? It is possible at all??

//Tom



( tags added)
[ May 16, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not think you can. The variable string in the testInner method is hidden by that class variable string defined in the method local class.

I suspect that there may be some tricky stuff possible using reflection but that's beyond the scope of SCJP.
[ May 16, 2005: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above, I have added a constructor which smuggles the method's string into the state of the inner class. Then you can access it from within the method InnerTestStrings. Mucky though.
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep that indeed will work since the method local variable shadows the outer class variable....but as you say its awkward. Dont really think the above situation arises very often anyways as it is bad naming convention, but wondering was there a way to do it
reply
    Bookmark Topic Watch Topic
  • New Topic