This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes multiple shadowing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "multiple shadowing" Watch "multiple shadowing" New topic
Author

multiple shadowing

Caroline Bogart
Greenhorn

Joined: Nov 21, 2001
Posts: 24
Khalid Mughal, p. 99
The variable indicator is defined at the instance, parameter and local levels.
In the code below, it looks (logically) like the two indicator's involved are the parameter and the local one (the code is ignoring this.indicator).
But the illogical part is that the parameter is receiving the assignment from the local.
Wouldn't it be more consistent if the local indicator received the assignment from the parameter indicator?

<PRE>
class Light {
// instance variables
boolean indicator;
public Light(boolean indicator) {

System.out.println("Before assignment indicator = " + indicator);
System.out.println("Before assignment this.indicator = " + this.indicator);
indicator = indicator;// assignment to parameter
System.out.println("After assignment indicator = " + indicator);
System.out.println("After assignment this.indicator = " + this.indicator);
}
public static void main(String args[]) {
System.out.println("Invoking light constructor setting indicator to true:");

new Light(true);


}
}
</PRE>

I love JavaRanch!
Caroline Bogart
Greenhorn

Joined: Nov 21, 2001
Posts: 24
Emily Littella! (never mind).
There is no "indicator" at the local level.
Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
Hi Caroline.What do you mean by parameter indicator.I think you are talking about the indicator which is in the parameter of constructor Light.That is
public Light(boolean indicator)
If you are taking about this indicator, then in the body of constructor if it is called as indicator then JVM will consider it as a local variable of constructor Light rather than instance variable defined in class as first statemnet in class lIght.If you want to call this instance variable then you may call it by this.indicator.
Hope This Helps.
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!


Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining http://groups.yahoo.com/group/IBM340Exam/
Caroline Bogart
Greenhorn

Joined: Nov 21, 2001
Posts: 24
Hi.
Originally I thought that "indicator" was defined in three places: instance, as a parameter to the Light constructor and as a local method (constructor) variable.
That's why I was confused. Then I realized that there is not local (constructor) variable indicator.
By "parameter indicator" I was referring to the parameter of the Light constructor.
The confusing code line is:
indicator = indicator;
which is apparently to be interpreted as:
indicator = this.indicator;
where the left-hand indicator is the parameter to the constructor, and the right hand indicator is the instance variable.
It made sense after I realized that there were two, not three "indicator" declarations.
Thanks!
Manoj Gupta
Greenhorn

Joined: Oct 31, 2001
Posts: 29
The output of the code as given will be
Invoking light constructor setting indicator to true:
Before assignment indicator = true
Before assignment this.indicator = false
After assignment indicator = true
After assignment this.indicator = false
Here, the code you have given the variable indicator is local to constructor and not the instance variable indicator. The statement indicator = indicator; assigns the same variable to itself.
And if you replace
indicator = indicator;
with
this.indicator = indicator;
the output will be:
Invoking light constructor setting indicator to true:
Before assignment indicator = true
Before assignment this.indicator = false
After assignment indicator = true
After assignment this.indicator = true
Here, the statement this.indicator = indicator; assigns the local variable indicator to the instance variable indicator. Result is evident in the output.
------------------
Cheers,
Manoj
(http://www7.brinkster.com/manoj9/)


Cheers, <img src="smile.gif" border="0"> <br /><a href="http://www7.brinkster.com/manoj9/" target="_blank" rel="nofollow">Manoj</a><br />(<a href="http://www7.brinkster.com/manoj9/" target="_blank" rel="nofollow">http://www7.brinkster.com/manoj9/</a>)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiple shadowing
 
Similar Threads
Question about constructor chaining...
Please Explain ...
hi waht is diffrence b/w this abstract method and overridding
Inherit private member
Constructor Chaining