aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about static variable question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about static variable question" Watch "about static variable question" New topic
Author

about static variable question

Senthil Somasundaram
Greenhorn

Joined: Jun 30, 2003
Posts: 17
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

Joined: Jun 02, 2003
Posts: 1872
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

Joined: Dec 20, 2003
Posts: 191
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

Joined: Oct 01, 2002
Posts: 5
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

Joined: Jun 30, 2003
Posts: 17
I got it!!!
Thanks a lot ranchers..
Smiles
Sen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about static variable question