title: Java. Abstract class child counter listener
body: <p>I have n child classes inherit from Parent. I need to count all child objects and when all will be done trigger listener. Unfortunately I'm getting an errors: "Static member 'childCountListener' accessed via instance reference" and "'this' cannot be referenced form a static context". I know I cannot use "this" because I don't have object of this class but I have no idea how to achieve this.</p>
  
  <p>Parent:</p>
  
  <pre><code>public abstract class Parent {
  
  protected static int childCount = 0;
  private static ChildCountListener childCountListener;
  
  public Parent() {
      // (...)
      incrementChildCount();
  }
  
  public void doSomething() {
      // (...)
      decrementChildCount();
  }
  
  public static int getChildCount() {
      return childCount;
  }
  
  private void incrementChildCount() {
      childCount++;
  }
  
  private void decrementChildCount() {
      childCount--;
  
      if (childCount &lt; 1) {
          childCountListener.allDone();
      }
  }
  
  public static void addChildCountListener(ChildCountListener childCountListener) {
      this.childCountListener = childCountListener;
      // Static member 'childCountListener' accessed via instance reference
      // 'this' cannot be referenced form a static context
  }
  
  public interface ChildCountListener {
      void allDone();
  }
  }
  </code></pre>