aspose file tools*
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
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  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: 19693
    
  20

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
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Hit counter