← Quiz: static, final and Nested Classes: all 10 questions
Quiz: static, final and Nested Classes · Question 8 of 10
What does this print? (code)
What does this print?
public class Main {
static int x = 10;
public static void main(String[] args) {
Main a = new Main();
Main b = new Main();
a.x = 20;
System.out.println(b.x + Main.x);
}
}
- A.20
- B.30
- C.40
- D.10
Answer & explanation
Correct answer: 40
x is static, so a.x, b.x and Main.x are the same variable. After a.x = 20 it is 20 everywhere: 20 + 20 = 40.
Can you get the rest right without peeking?
Take the timed Quiz: static, final and Nested Classes quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →