• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Hit counter

 
Ephraim Paka
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to count the number of messages getting passed to my processMesessage() method

Code snippet below:

public static int messageCounter;

processMesessage(String message)
{

synchonized(this) {++messageCounter;}

System.out.println(" Number of Messages" + messageCounter) }

}

Every times its printing only 1

Any thoughts?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33691
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ephraim,
How are you running the method? Is it a standalone Java program that launches each time? If so, it is in a different JVM and the static fields start over.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ephraim Paka wrote:
Any thoughts?


Yep! My thoughts are that there's something wrong with your code, but since this isn't your real code, we can't see what it is. Unless you show us the actual code, there's nothing we can do to help you.
 
Ephraim Paka
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Ephraim,
How are you running the method? Is it a standalone Java program that launches each time? If so, it is in a different JVM and the static fields start over.


Hello,

I'm running it as a javabean ( on websphere61.) and calling the processMessage() in the class initializer. Its the same JVm but the static field seems to be getting initialized/getting reset to 0 for each instance creation!

E
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try printing out which class loader you're using:
If you see the class loader changed (which the second print statement will definitely be able to tell you), then that means the class is actually a different class* with its own static fields.

* Or at least, a different Class object.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic