Is it possible to calculate How many and What objects created by a Class?

Here I created an Object for C class, due to inheritance mechanism it may create totally Three objects(C,B and A ). I calculated by viewing code. But I want to calculate by programmatic-ally. I mean, Is it possible to calculate how may Objects created by My Class and What they are?

Below is my code:

class A {
A() {       
    System.out.println("class A");
}
}
class B extends A {
B() {        
    System.out.println("class B");
}
}
class C extends B {
public static void main(String args[]) {
    C c = new C();
}
}

Answers




    class A {
    static int a;
    {       
        System.out.println("class A , "+ (++a));
    }
    }
    class B extends A {
    static int b;
    {        
        System.out.println("class B ,"+ (++b));
    }
    }
    class C extends B {
    static int c;
    {
       System.out.println("class C ,"+ (++c));
    }
    public static void main(String args[]) {
        C c = new C();
    }
    }


Instead of incrementing counter in constructor , use init block to increment counter. This will help you when you are not creating objects using default or no-arg constructor.


Not "automatically", but you can add code to do it:

class A {
    private static List<A> instances = new ArrayList<>();     
    A() {
        if (instances.size() > 2)
            throw new IllegalStateException("may not create more than 3 instances");
        instances.add(this);
        System.out.println("class A");
    }
    public static List<A> getInstances() {
        return instances;
    } 
}

You can try this way.

public class   A {
static int i;
List<Integer> instanceA=new ArrayList<>();
A() {
    instanceA.add(++i);
    System.out.println("class A");
}
}

.

public class B extends A{
static int i;
List<Integer> instanceB=new ArrayList<>();
B() {
    instanceB.add(++i);
    System.out.println("class B");
  }
}

.

public class C extends B {
static int i;
List<Integer> instanceC = new ArrayList<>();

C() {
    instanceC.add(++i);
}

public static void main(String args[]) {
    C c = new C();
    System.out.println("Number of C"+c.instanceC);
    System.out.println("Number of B"+c.instanceB);
    System.out.println("Number of A"+c.instanceA);
 }
}

If you are create two instance of C like follows.

    C c = new C();
    C c1=new C();
    System.out.println("Number of C"+c1.instanceC);
    System.out.println("Number of B"+c1.instanceB);
    System.out.println("Number of A"+c1.instanceA);

You will have the out put as follows

   class A
   class B
   class A
   class B
   Number of C[2]
   Number of B[2]
   Number of A[2]

How many objects are created are created of a particular class can be found by having a counter of static int type . Like :

Class A {
 public:  static int count = 0;
A(){
// inside constructor increment the counter for each constructor call
count ++;
}
void totalCount(){
System.out.print(count);
}
}

Then call totalCount() method to display number of objects created. Hope this helps..


Need Your Help

jQuery .animate() not doing anything in IE8

jquery internet-explorer-8 jquery-animate opacity

I have a div of text (#text-write), each character of which is spanned. The div is absolutely positioned inside a relatively positioned container div (#typetext). Here's the effect intended:

Comments for Function in Emacs

emacs comments elisp doxygen doc

I'm looking for a way to generate and insert header comment blocks above my functions in Emacs (in any mode), with the default contents of the comment automatically based on the function's signatur...