You can use a private instance variable counter, that you can increment on every call to your method: –
public class Demo {
private int counter = 0;
public void counter() {
++counter;
}
}
Update : –
According to your edit, you would need a static variable, which is shared among instances. So, once you change that varaible, it would be changed for all instances. It is basically bound to class rather than any instance.
So, your code should look like this: –
class Anything { // Your class name should start with uppercase letters.
private static int counter = 0;
public String toString() {
++counter;
return "the time this method is being called is number " +counter;
}
}
0
solved How can I tell if a method is being called?