Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Method Hit counter

 
Ranch Hand
Posts: 37
  • 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?
 
author & internet detective
Posts: 39540
778
Eclipse IDE VI Editor Java
  • 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.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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
  • 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
 
Sheriff
Posts: 21805
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • 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.
 
crispy bacon. crispy tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!