• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

about static variable question

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it!!!
Thanks a lot ranchers..
Smiles
Sen
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic