Re: What happens to x




"Ravi" <ra.ravi.rav@xxxxxxxxx> wrote in message
news:1177876365.913340.26180@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
what will be the effect of the following code on variable x, and why:

class Sup {
int x=10;
void set(int a) {
x = a;
}
}

class Sub {
private int x;
}

class Demo {
public static void main(String args[]) {
Sub s1 = new Sub();
s1.set(2)
}
}


did you mean to say "class Sub extends Sup" and "protected void set"?

if so then when you create Sub with "new", x will be 10, after the call to
set it will be 2


.