Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about static variable question

 
Senthil Somasundaram
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Philippe Maquet
Bartender
Posts: 1872
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sen,
As connectionCounter is static, both cp.connectionCounter and pool.connectionCounter refer to the *same* variable.
If you swapped lines 11 and 12, the answer would be "0 0".
Regards,
Phil.
 
Vineela Devi
Ranch Hand
Posts: 191
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sen,
answer is correct and what phil explained is absolutely rigth.
As the varisble connectionCounter is declared static, there exists only one copy of this variable for all the objects. separate copy doesnot exist for each object.
so, for the first time when u create an object cp , value of connection counter variable will be 1(since u called the increment() method in the constructor).When u set the connectionCounter to 0 at line 11, the value of the connectionCounter will be 0.
Again when u create one more object of the class ConnectionPool then again the connectionCounter variable is set to 1( same as explained above).
since there exists only one copy of the static varaible , both cp.connectionCounter and pool.connectionCounter will be 1.
Hope this will help u.
 
larmie santos
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer is right, the static variable is shared by all the objects you create from that class, so if you change it to the first object then the second object is definitely affected...
 
Senthil Somasundaram
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it!!!
Thanks a lot ranchers..
Smiles
Sen
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic