• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

questions about static keyword ?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the output of the below code is 25 , 1
but when i write

then the output is 25 , 25
so by writing static does it maintain only one copy of counter variable , no matter how many object is being created ,
and if any new object is being created of that particular class type then it will have the latest value of counter variable , right ?
 
Ranch Hand
Posts: 32
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can consider a static field to belong to the class itself rather than to an instance. So for the sake of the argument, yes, there is only one single counter variable. Your instances will access that singleton counter, so they won't just 'have the latest value', but actually access the same thing.

Note, however, that a static counter variable is widely considered to be bad practice. It is, of course, fine to use it for debugging purposes, but a non-final public static field may introduce problems - for instance, when you're using multiple threads in your application (two threads accessing the same variable may lead to inconsistencies which are incredibly hard to debug).
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexander Kober wrote:You can consider a static field to belong to the class itself rather than to an instance. So for the sake of the argument, yes, there is only one single counter variable. Your instances will access that singleton counter, so they won't just 'have the latest value', but actually access the same thing.

Note, however, that a static counter variable is widely considered to be bad practice. It is, of course, fine to use it for debugging purposes, but a non-final public static field may introduce problems - for instance, when you're using multiple threads in your application (two threads accessing the same variable may lead to inconsistencies which are incredibly hard to debug).


thanks
reply
    Bookmark Topic Watch Topic
  • New Topic