• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Anonymous inner access to shadowed method argument

 
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

This is one of those silly thoughts that I can't think of a good reason for doing, but now I'm wondering if one can do it anyway! I checked the JLS, but failed to find anything relevant--that doesn't mean it's not there, of course!

Suppose I have (and bear with the silly example, it's illustrative of the question, not "the problem I need to solve")



Obviously, the simple answer is "don't use the same variable names", and I broadly concur. However, out of interest, Java has some funky syntaxes for this kind of thing (like accessing shadowed members in an enclosing class) , and I'm wondering if there's one for this?

TIA,
Toby

 
Sheriff
Posts: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no way. You can access members of any enclosing class (using EnclosingClass.this.member), but not method parameters or local variables of any enclosing method.
 
Toby Eggitt
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fair enough, thanks. I suspected this, but realized I'd never actually thought about it before, so wanted to check if I'd missed something. It's hard to distinguish the spec being silent on something from failing to find it.

Cheers,
Toby.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you weren't modifying the variable then you could:

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Doable", that looks like "Double" but spelled wrong...
 
Toby Eggitt
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you weren't modifying the variable then you could...

I know ;) That's why I made sure to modify the variable, even in the most trivial example.

Actually, that's what led me to wonder. I was using the arguments as initialization values, which was why using the exact same name has "some merit", it's the same situation as would be normal with constructor arguments where you end up with



and I will happily assert that if the two variables actually identify the exact same meaning, then using the same name is reasonable, therefore this can be a good thing(tm). But it's also not a "must have" either.

...

"Doable", that looks like "Double" but spelled wrong...



Hmm, it does, doesn't it! Glad that didn't get past code review!
 
yeah, but ... what would PIE do? Especially concerning this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic