Originally posted by kotha vijaybabu:
then what about this
class X {
String a;
void s(String a) {
this.a="second";
System.out.println("in method() "+a);
}
public static void main(String args[]) {
String a="first";
X x1=new X();
x1.s(a);
System.out.println("in main() "+a);
}
}
Each time you create an instance of a class definition, space is reserved for each instance variable. In the method, you are setting the instance's copy of s to "second". However, because you have a local variable named s, it shadows the instance variable s. So that is why the method prints "first".