0

In Java, if I want to know how many object of type MyClass, i can define the MyClass in this way

public class MyClass {

    public static int count = 0;

    public MyClass() {
        count++;
    }

    //other stuff
    //...
}

and then, just calling

MyClass.count

I can get the number of objects created.

I am wondering if there's a way to do the same thing with an interface, e.g. if I have my interface called ICountable, how can I know how many objects that are ICountable are there in my program at that moment. I am thinking of doing this with a factory pattern, but in any design way I notice weaknesses, so I haven't come up to a working solution yet, does anyone know a good way to do this?

vcmkrtchyan
  • 2,536
  • 5
  • 30
  • 59
  • You know that you are going to have to `implement` your interface first? So I am assuming that you will be using an object of a class that `implements` your iCountable interface? – ha9u63a7 Oct 27 '14 at 10:14
  • 1
    Unless you can exert *some* control over the people implementing your interface, it's not possible unless you're willing to turn your interface into an abstract class. But since Java has single-implementation-inheritance, that might interfere with other things. – Sebastian Redl Oct 27 '14 at 10:16
  • yeah of course, but how can I make it mandatory to implement the counting system for all classes that implement my interface? How can I get the number of all ICountable objects without counting separately for all the implementing classes and summing it up? – vcmkrtchyan Oct 27 '14 at 10:17
  • @SebastianRedl I tried to do that with an abstract class, but it's not what is needed, because java does not allow multiple inheritance, so it kinda blocks me – vcmkrtchyan Oct 27 '14 at 10:19
  • 1
    @A4L That should be an answer. – João Mendes Oct 27 '14 at 10:26
  • @JoãoMendes Question is about counting class instances, not counting classes! I don't see how a custom classloader would help here? – Gyro Gearless Oct 27 '14 at 10:40
  • @GyroGearless I imagine there would be several ways. Off the top of my head, a class loader can analyze each loaded class and, if it implements the interface, inject code into the constructors via reflection. Or maybe it could return a proxy to the class, with the adequate counters already implemented. And I'm sure there would be more solutions. – João Mendes Oct 27 '14 at 11:06

3 Answers3

0

You could have a look at this thread:

how many instances for a class exists at runtime in java

But I recommend, that you do not implement such a mechanism. There is practically no use case for it in release code. And for debugging / profiling there are several tools. If I remember correctly, the Eclipse profiler keeps track of the instances out of the box, just start it and watch.

Community
  • 1
  • 1
UniversE
  • 2,419
  • 17
  • 24
0

Refering to my answer in Get Member/Fields of an existing Object as faar as i know there is no way to guarantee getting all of such informations without parsing the classpath on the file to analyse any .class files or (e.g. static code analysis).

Even if there are classes implementing the desired interface those classes may not be directly visible/ accessable by your Classloaders (anonymous classes for example).

Still in my anser there is a library mentioned that most of the times does the job.

Community
  • 1
  • 1
JBA
  • 2,769
  • 5
  • 24
  • 40
0

There can be several implementation for single interface. All implementation should consider as type of that interface.

You can try something similar to this.

My interface

public interface Val {}

Implementations

public class Impl1 implements Val {
 public Impl1(){
     Con.count++;
 }
}

.

public class Impl2 implements Val {
    public Impl2(){
        Con.count++;
    }
}

You need to use a global counter here.

Eg:

public final class Con {
 static int count;
}

Now any time you can find the count of Val

public static void main(String[] args) {
     new Impl1();
     System.out.println(Con.count);
     new Impl2();
     System.out.println(Con.count);
 }

you can use separate variables in Impl1 and Impl2 to count each of them separately too.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • yeah, but what i was talking about is that anyone can write a class implementing my interface and not implementing the counting system, so i will have wrong number – vcmkrtchyan Oct 27 '14 at 10:53