Campbell Ritchie wrote:Welcome to the Ranch
I shall move you to our exams fora.
Please tell us which questions you got wrong and why. Start with not more than five and let us see what you have done.
Thank you. Id be more than happy
Here is a sample code i don't understand :
public class Test {
static int count = 0;
int i = 0;
public void changeCount() {
while (i < 5) {
i++;
count++;
}
}
public static void main(
String[] args) {
Test check1 = new Test();
Test check2 = new Test();
check1.changeCount();
check2.changeCount();
System.out.println(check1.count + " " + check2.count);
}
}
Please explain to me why the answer is 10 10 .
I thought the answer is 8 8, since the while loops checks if i<5. So its true only if i<5 therefore count is only incremented 4 times. I understand that the same static variable 'count' is shared among all objects.