Search code examples
design-patternsstatic-methodsobserver-pattern

Keeping track of static method calls in a class using java.util.Observer


I'm a beginner programmer, and am wondering how to work around this issue. The problem I am trying to solve is keeping a tally of static method calls in my program using a java.util.Observer.

Clearly, my original post must have just confused people so I'll leave it more open-ended: can anyone suggest a way to keep a static method count on a class that extends Observable?

Thanks for looking


Solution

  • Thanks for the response. It was helpful. I solved this by adding a static method to my class which extended Observer, so that it has two update methods:

    public static void update(Method method)
    {
        //update count here
    }
    
    @Override
    public void update(Observable observable, Object obj) {
                //override code to handle observable's call to update
    
        }
    

    Whenever I ran into a static method that I needed to keep a count of, I called MyClass.update() and passed a reference to the calling Method using Reflection API's getEnclosingMethod().

    ex:

    MyObserver.update(new Object(){}.getClass().getEnclosingMethod());
    

    Seems to work ok.