This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Please Explain ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please Explain ..." Watch "Please Explain ..." New topic
Author

Please Explain ...

Prashanth Lingala
Ranch Hand

Joined: Nov 13, 2004
Posts: 66
class Light
{

int noOfWatts;
boolean indicator;
String location;

public Light(int noOfWatts, boolean indicator, String site) {

String location;

this.noOfWatts = noOfWatts;
indicator = indicator;
location = site;
this.superflous();
superflous();
}

public void superflous() { System.out.println(this); }


public static void main(String[] args)
{
Light light = new Light(100, true, "loft");
System.out.println("No. of watts: " + light.noOfWatts);
System.out.println("Indicator : " + light.indicator);
System.out.println("Location : " + light.location);
}
}

/*
Output:

Light@3
Light@3
No. of watts: 100
Indicator : false
Location : null

Press any key to continue...
*/

============================================================================
Can SomeBody Explain Me Why I Am Getting This Output ???
This Has Gone Way Over My Head...
(Source: Java Certification, Khalid Mughal)...


Have A Nice Day !!!
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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 ]
Prashanth Lingala
Ranch Hand

Joined: Nov 13, 2004
Posts: 66
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,
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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
Correct!

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 ]
Prashanth Lingala
Ranch Hand

Joined: Nov 13, 2004
Posts: 66
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...

Thank You

Regards
Prashanth Lingala
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Yes, you have it! It is possible to assign ANY variable to itself, although you typcially don't need (or even want) to do this.

Layne


Java API Documentation
The Java Tutorial
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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:
san ch
Ranch Hand

Joined: Oct 18, 2004
Posts: 68
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
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I'm not sure. From your description, that behvior seems odd to me. Can you copy-and-paste the whole (modified) constructor here?

Thanks,

Layne
san ch
Ranch Hand

Joined: Oct 18, 2004
Posts: 68
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

i hope iam clear :roll:
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Because you still have the location variable declared in your constructor.

remove that and you won't need the this.
san ch
Ranch Hand

Joined: Oct 18, 2004
Posts: 68
hey thank you steven ....didnt see the code properly while i copied it should look closely
thank you for your time
san
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please Explain ...
 
Similar Threads
multiple shadowing
hi waht is diffrence b/w this abstract method and overridding
Question about constructor chaining...
Inherit private member
Constructor Chaining