• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StatFanta

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pl comment,
class StatFanta {
public static int counter ;
public StatFanta() {
counter++;
}
}
public class StatFantaClear{
public static void main (String [] argv){
StatFanta SF1 = new StatFanta();
StatFanta SF2 = new StatFanta();

int a = SF1.counter;
int b = SF2.counter;
System.out.println(a);// gives 2
System.out.println(b);// also gives 2, ??
}
}
if i set counter to 0, i get
Exception in thread "main" java.lang.NosuchMethodError

------------------
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that as the variable counter is static, there is only one incarnation of counter regardless of the number of instances of the class that contains it (it could event be zero).
So you are referring to same counter each time you ++ it.
How did you set counter to zero? (BTW, topic seems to be a little strange?!)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic