-2

Suppose I have a demo.java class and have other classes like A.java , B. Java and so on.

I want to write some code in Demo.java to get the count , that will tell in how many classes Demo.java is getting called.

Any suggestions would help. Thanks in advance!

Kaustubh
  • 506
  • 4
  • 22
  • You mean, how many times it gets instantiated?? OR you considering method call also – Tarun Feb 11 '19 at 08:42
  • @Tarun: Yes! I mean how many times it gets instantiated. – Kaustubh Feb 11 '19 at 08:44
  • But of course: A) an ordinary simple int counter isnt thread-safe, you should use AtomicInteger instead if that is important B) the whole idea of counting instances is nothing you do in the real world. – GhostCat Feb 11 '19 at 08:50

3 Answers3

1

Create a static variable in class.
public static int callerCounter=0;.

And then increment it in every constructor. E.g:-

Demo()
{
callerCounter++; // Add this line in every Constructor
}

And Print callerCounter where you want to get counter value.

Tarun
  • 986
  • 6
  • 19
  • Thanks, Varun! I had tried this. This works if I create the object in Demo class itself. But I call Demo.java in a separate class file say A, then it won't work. – Kaustubh Feb 11 '19 at 08:53
  • @Kaustubh Make the static variable public, Since you may be calling it from different package – Tarun Feb 11 '19 at 09:01
  • Thanks. I made the static variable public. But still, it's not working. As you suggested, if add the counter line in every class constructor how will I get the total count? – Kaustubh Feb 11 '19 at 09:08
  • @Kaustubh The static variable will only store count till the program Or caller class gets terminated. If you want counter that will save count number after programs gets terminated. Then you need to use Database or write counter in file. – Tarun Feb 11 '19 at 09:15
  • I would make callerCount AtomicInteger for thread safety. – pablosaraiva Feb 11 '19 at 12:04
0

Create a static Set callerClasses to hold the name of each class that called your Demo class.

Then, at each call to your Demo class, add the caller name to the set.

At any point in time you can check how many different classes called your Demo class by inspecting the Set size.

EDIT NOTE #1:

The question was made clear after I posted my answer that the intention is not count for calls on methods but count instances created.

I will keep it anyway because this may be the case for someone else.

EDIT NOTE #2:

Added code sample for completeness.

  public class Demo {

  // ConcurrentSkipListSet for thread safety
  private static Set<String> callerCount = new ConcurrentSkipListSet<>();

  public void methodA() {
    String className = new Exception().getStackTrace()[1].getClassName();
    recordCaller(className);
  }

  public long getNumberOfCallers() {
    return callerCount.size();
  }

  private void recordCaller(final String className) {
    callerCount.add(className);
  }
}
pablosaraiva
  • 2,343
  • 1
  • 27
  • 38
0

I do not think it is doable in general case by just analyzing the code. If the interface is in use, may not be trivial to know which class will be instantiated behind the interface. This may depend on decision within some factory and may influenced by the input that is only available at runtime, well, even by SecureRandom theoretically.

You can always put either counters or just logging statements (processing the log output separately) to collect this kind of statistics at runtime. Tools like JProfiler may work as programming-free alternative.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93