============================================================================ Can SomeBody Explain Me Why I Am Getting This Output ??? This Has Gone Way Over My Head... (Source: Java Certification, Khalid Mughal)...
I'll give you a few hints so that hopefully you can figure it out yourself. The first two lines, however, need to be simply explained.
When you pass an object to System.out.println() or add it to a String ("Hi " + light), Java calls the toString() method on it to create a text representation of it. Because Light doesn't have its own toString() method, the one defined in its superclass (Object) is called.
Object.toString() returns the name of the class followed by an "@" and a hexadecimal number based on its memory address. When I run the program, I see "Light@187c6c7".
I leave it to you to figure out why it is printed twice.
The key to the rest of the output is in Light's constructor, the "public Light(...) method. The values you see are the result of many factors: default class field values, variable hiding/shadowing, and the difference between method arguments (int noOfWatts, boolean indicator, String site) and class fields.
As a starter, compare these two lines:Can you see how they differ other than the names of the variables? And how does that affect the output they generate?
Play around with these hints a bit and alter the code to see if you understand what's happening. If you still have doubts, come back and ask specific questions and tell us what you've tried out and what you don't understand.
Good luck! [ January 29, 2005: Message edited by: David Harkness ]
Joined: Nov 13, 2004
I tried following the code in the light of your suggestions... However consider this,
why should we not write
when i use this, i get boolean value as true, and i feel
should also give true, kindly elucidate, In addition to this, i presume this.superflous() and superflous() are one and the same, hence the result printed Light@3, twice
ThankYou for helping me out,
Joined: Aug 07, 2003
You're welcome. I'm always glad to help when the student is eager to learn.
Originally posted by Prashanth Lingala: why should we not write
That's exactly what should be used.
and i feel
should also give true,
No. What is the difference between "this.indicator" and "indicator"? What does each one refer to (hint: they do not refer to the same variable). Maybe this will help: does the second line in this next block have any effect?If so, what does it do?
Do you see why "Location : null" is printed instead of "Location : loft"? The reasoning is very similar to the case of "Indicator : false". The cases differ, but only slightly. It is important to understand them, however.
i presume this.superflous() and superflous() are one and the same, hence the result printed Light@3, twice
When calling methods on the same object from within another method of that object (as is the case here), the "this." is, well, superfluous. In the constructor, "superfluous()" means "call the superfluous method on this object" -- "this.superfluous()" means axactly the same thing.
Some people prefer to use the "this." as a habit to improve clarity; you're explicitely telling any person reading the code (since the compiler can figure it out) that the same object will receive the method call. [ January 29, 2005: Message edited by: David Harkness ]
Joined: Nov 13, 2004
Dear Sir, when i use
i get value as "loft",Light has a String variable location, so i suppose "loft" is passed to this variable, only when i explicitly make use of this... If i had not used this then site's value would be assigned to the constructor's local variable, and Light's location would be by default "null"...
now for the indicator part... if i use this.indicator = indicator i get the result as true...
Is it possible to assign a parameter variable to itself... If the answer is YES, then all my queries have been solved...
Originally posted by Prashanth Lingala: Is it possible to assign a parameter variable to itself... If the answer is YES, then all my queries have been solved...
Right again and again.
In fact, there is a keyword in Java that can help you not make this mistake in your own code: "final". The final keyword means that the variable can be set only once and never be changed. If the constructor had been declared asthen the compiler (javac) would have reported an error for this line:You'll find that people rarely use this for parameters (just laziness I suspect -- at least that's the case for me ). But it is definitely used for constants: variables that you use throughout your program but never change. For example:
Hi i was jus working with the code given here i see that if we use different parameter names in the say
i changed the above line to
and it works without using this however the string is not being printed properly .....now i can uderstand that when the variable names are same it will have a problem with resolving without having "this" but why am i not able to get the string name alone when i changed the paramaternames ......i can display the string passed properly if i use
can you pls explain why this is happening thank you san
Joined: Dec 06, 2001
I'm not sure. From your description, that behvior seems odd to me. Can you copy-and-paste the whole (modified) constructor here?
Joined: Oct 18, 2004
sorry for the hazy description
the original code is like this
i have changed the paramaters in the constructor like this
the code works normally and prints what i pass as parameters but however it doesnt print the location ..it gives the location as NULL
now if i change the statement for the location in the constructor as
it prints the string i pass i didnt understand why it is not printing without prefixing with the "this" where it is printing for the other variable of the class