0

i know that this works in C/C++ but somehow I wont find a way in java to get this working.

I have about 50 methods and I want to count how many times i called them. It would work if I made 50 variables outside the method and inside I simply increment them.

But that are way to much variables. Is there a way to handle this with one variable inside the function? Or with one variable outside the methods.

I need just a way to do this with one variable.

Has anyone an Idea?

2
  • arrays to your rescue. Commented Apr 27, 2016 at 13:25
  • 3
    Use a HashMap<String, Integer>? Commented Apr 27, 2016 at 13:25

2 Answers 2

1

Map<String,Integer> counters = new HashMap<>();

String is the method name (or whatever you want it to be) and Integer is the counter.

Just declare it outside your functions, and increment each value in the Map when you are hitting your method (according to the key, which is the method name)

Sign up to request clarification or add additional context in comments.

Comments

0

Decalare this where its available to access from.

Map<String, Integer> callCount=new HashMap<String,Integer>();

Then In every method do something like:

public void anyMethod()
{
    Integer count=callCount.get("anyMethod"); 

    if(count==null)
    {
        count= new Integer(0);
    }

    count++;
    callCount.put("anyMethod",count);

    /**
    other things that your method do......
    **/
}

1 Comment

don't have IDE now so please ignore any typo if there any.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.