Hi ranchers,
I am preparing for the SJCP.When i attend mock exam on javacertificate.com.
In Following Quetion static variable connectionCounter
is incremented in non static method increment().
1.public class ConnectionPool {
2. public static int connectionCounter = 0;
3. public ConnectionPool() {
4. increment();
5. }
6. private void increment() {
7. connectionCounter++;
8. }
9. public static void main(
String[] args) {
10. ConnectionPool cp = new ConnectionPool();
11. cp.connectionCounter = 0;
12. ConnectionPool pool = new ConnectionPool();
13. System.out.println (cp.connectionCounter + " " +
14. pool.connectionCounter);
15. }
16.}
1. Compilation succeeds, the output is 0 1
2. Compilation succeeds, the output is 1 1
3. Compilation succeeds, the output is 1 0
4. Compilation succeeds, the output is 0 0
5. Compile time error, the member variable connectionCounter is not visible.
Ans 2
Is this one correct? If yes please explain the reason.
Thanks,
Sen