title: Java PropertyChangeListener
body: <p>I'm trying to figure out how to listen to a property change in another class. Below is my code:</p>
  
  <p>ClassWithProperty has the property I want to listen to:</p>
  
  <pre><code>public class ClassWithProperty {
      private PropertyChangeSupport changes = new PropertyChangeSupport(this); 
      private int usersOnline;
  
      public int getUsersOnline() {
          return usersOnline;
      }
  
      public ClassWithProperty() {
          usersOnline = 0;
          while (usersOnline&lt;10) {
              changes.firePropertyChange("usersOnline", usersOnline, usersOnline++);
          }
      }
  
      public void addPropertyChangeListener(
          PropertyChangeListener l) {
          changes.addPropertyChangeListener(l);
      }
      public void removePropertyChangeListener(
          PropertyChangeListener l) {
          changes.removePropertyChangeListener(l);
      }
  }
  </code></pre>
  
  <p>Main is where i need to know about the property change:</p>
  
  <pre><code>public class Main {
  
      private static ClassWithProperty test;
  
      public static void main(String[] args) {
          test = new ClassWithProperty();
          test.addPropertyChangeListener(listen());
  
      }
  
      private static PropertyChangeListener listen() {
          System.out.println(test.getUsersOnline());
          return null;
      }
  
  }
  </code></pre>
  
  <p>I have the event fired only the last time (usersOnline=10).
  I'm new to Java and tried to find a solution, but to no avail.</p>