• Post Reply Bookmark Topic Watch Topic
  • New Topic

Anonymous inner access to shadowed method argument  RSS feed

 
Toby Eggitt
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • 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

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 60
1
  • Mark post as helpful
  • send pies
  • 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.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you weren't modifying the variable then you could:

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Doable", that looks like "Double" but spelled wrong...
 
Toby Eggitt
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • 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!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!