Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Method Hit counter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Hit counter" Watch "Method Hit counter" New topic
Author

Method Hit counter

Ephraim Paka
Ranch Hand

Joined: Apr 12, 2007
Posts: 37
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

Joined: May 26, 2003
Posts: 32508
    
214

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.


[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24190
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Ephraim Paka
Ranch Hand

Joined: Apr 12, 2007
Posts: 37
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

Joined: Oct 27, 2005
Posts: 20083
    
  30

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Method Hit counter
 
jQuery in Action, 3rd edition