Hello I just have this simple question public class Bll { String s ; int a; Bll(){ this (100, "Ragu"); } Bll(int a, String s) { a = a; s = s; } public static void main (String args[]) { Bll b = new Bll(); System.out.println(b.s); } } The output returns null If i change it to this.s=s; this.a=a; The output is Ragu Though i understand the reason for the outputs, i guess what i am wondering is what exactly is happening when its declared a=a; s=s; inside the constructor? Based on the output the basically the assignment havent even taken place? Gurus, please explain... Thankx
Guy Allard
Ranch Hand
Joined: Nov 24, 2000
Posts: 776
posted
0
Well, assignments have taken place. But you are just assigning a parameter variable to itself. The fact that you have a parameter variable (reference) with the same name as a instance variable hides the instance variable. So: a=a; // assign inbound a to inbound a this.a=a; // assign instance variable to inbound HTH, Guy
chao-long liao
Ranch Hand
Joined: Jul 29, 2001
Posts: 78
posted
0
Excuse me,I don't understand about the code. There is no implicit call (this) at the line a=a;why?? What situiation will call implicit (this)?? ---------------------------------- thanks for help,Liao
Ragu Sivaraman
Ranch Hand
Joined: Jul 20, 2001
Posts: 464
posted
0
Originally posted by Guy Allard: Well, assignments have taken place. But you are just assigning a parameter variable to itself. The fact that you have a parameter variable (reference) with the [b]same name as a instance variable hides the instance variable. So: a=a; // assign inbound a to inbound a this.a=a; // assign instance variable to inbound HTH, Guy[/B]
Well, "this = a" assign instance variable to bound, ok thats fine but, if "a=a" is assigning inbound then when i print out a or s how come iam getting 0 or null?
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Hi Ragu, <code>0</code> is the default value for an <code>int</code> and <code>null</code> is the default for a <code>String</code>. These are assigned to the instance fields <code>int a</code> and <code>String s</code> when you create the new <code>Bll()</code> object. As Guy pointed out, the parameters you are using in the second constructor are hiding the instance variables; it is not assigning any values to them. Therefore, when you call <code>System.out.println(b.s)</code>, <code>s</code> still has it's default value <code>null</code> and that's what you see. For more information on hiding see JLS §8.3.3.1 Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform [This message has been edited by Jane Griscti (edited August 12, 2001).]