This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
In the past I've always taken the implicit "this" variable for granted, that is to say in a given instance it's a reference back to that instance.
Yesterday, whilst doing some technical online tests, I came across this notation, where "this" is seemingly referenced via a static member of the class type ( note this is how it seems from the syntax, not sure what actually is going on ), e.g.
Returning this.name in the toString method is self explanatory, but the Employee prefix is throwing me, the syntax implies that "this" is a static member of the Employee class, but this doesn't make sense given that the whole point of "this" is that is it's a reference per instance.
Typically, you see syntax like that in Inner Classes, where the "this" reference would normally refer to the instance of the inner class instead of the instance of the enclosing class. If you prefix "this" with the enclosing class, you now have a reference to that instance.
Note that in the line labeled 20, there is a MyPanel.this.doAction() call. You could just say "doAction()" and the compiler will figure it out -- but I don't like it because it breaks the subject-verb paradigm that I program in (but that's an argument for another thread).
But you can't say "this.doAction()," as the "this" refers to the instance of the anonymous ActionListener that we are creating. So, in order to get "this" to reference the MyPanel instance, we have to say "MyPanel.this.doAction()"
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joined: Jul 03, 2007
Ah yes - thanks.
Makes perfect sense in context of inner classes - I guess I was thrown as I'd never seen this syntax outside of inner classes.