Originally posted by mari:
Hi all,
Check the following code,
I thought, since sName is a static variable the ans would be "Joey01",
but it is "Joey" - i am surprised, could someone explain
thanks in advance.
public class Tux{
static String sName = "Joey";
public static void main(String argv[])
{
Tux t = new Tux();
t.runM0(sName);
System.out.println(sName);
}
public void runM0(String sName)
{
sName = sName + "0";
runM1(sName);
}
public void runM1(String sName)
{
sName = sName + "1";
}
}
Also is it safe to say that, since sName is static and it is shadowed during the modifications. These modifications would not be reflected at t.runM0(sName), and System.out.println(sName).
But what about the sName in the runM0 and the runM1 methods, are they being shadowed ?
And also if the sName was private, and we were trying to set the value of sName through public. Does this mean we have to use this keyword with sName, so the value would be modified.