title: Observable in Java
body: <p>I'm trying to understand the Observer and the Observable.</p>
  
  <p>Here's an example that I'm trying to figure out:</p>
  
  <pre><code>public class IntegerDataBag extends Observable implements Iterable&lt;Integer&gt; {
  
      private ArrayList&lt;Integer&gt; list= new ArrayList&lt;Integer&gt;();
  
      public void add(Integer i){
          list.add(i);
          setChanged();
          notifyObservers();
      }
  
      public Iterator&lt;Integer&gt; iterator(){
          return list.iterator();
      }
  
      public Integer remove (int index){
          if (index&lt; list.size()){
              Integer i = list.remove(index);
              setChanged();
              notifyObservers();
              return i;
          }
          return null;
      }
  
  }
  
  public class IntegerAdder implements Observer {
  
      private IntegerDataBag bag;
  
      public IntegerAdder(IntegerDataBag bag) {
          this.bag = bag;
          bag.addObserver(this);
      }
  
      public void update(Observable o, Object arg) {
          if (o == bag) {
              System.out.println("The contents of the IntegerDataBag have changed");
          }
      }
  
  }
  </code></pre>
  
  <ol>
  <li><p>The <code>bag.addObserver()</code> can be made only because <code>IntegerDataBag</code> extends <code>Observable</code>?</p></li>
  <li><p>Where is this observer being add to? What is being created and where?</p></li>
  <li><p>What is the difference between <code>setChanged()</code> and <code>notifyObservers()</code>?</p></li>
  <li><p>I don't understand the <code>update</code> method; what does <code>arg</code> stand for? Why do I need to check that <code>o==bag</code>? Why would I update another observable? </p></li>
  <li><p>Why should I need this observer anyway?</p></li>
  </ol>