This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
An example like this is in the SCJP 6 study guide (pg 55) but doesn't give a clear enough explanation (for me at least) of what's going on. Why, in the setSize method, does the left hand side's "size" not refer to this.size implicitly? In the setSize2 method it does???
Just when I think that I am beginning to understand some of this java stuff, it goes horribly wrong.
Gil Steidle wrote:... Why, in the setSize method, does the left hand side's "size" not refer to this.size implicitly? In the setSize2 method it does??? ...
Because "setSize" method has a local variable called "size" which shadows the instance variable "size" whereas in "setSize2" method "size" resolves to the instance variable. And remember in the first case both side refers to the same (local) "size" variable. It's the scope what matters, it uses the nearest scope.
There's no difference between a left-hand and a right-hand operand in terms of what variable is being referred to. in setSize, 'size' refers to the method parameter. If you want to reference the instance variable 'size', you will have to refer to it explicitly by using this.size. You don't have to do this in setSize2 because the instance variable isn't being shadowed by the method parameter.