title: Java Pass Method as Parameter
body: <p>I am looking for a way to pass a method by reference. I understand that Java does not pass methods as parameters, however, I would like to get an alternative. </p>
  
  <p>I've been told interfaces are the alternative to passing methods as parameters but I don't understand how an interface can act as a method by reference. If I understand correctly an interface is simply an abstract set of methods that are not defined. I don't want to send an interface that needs to be defined every time because several different methods could call the same method with the same parameters.</p>
  
  <p>What I would like to accomplish is something similar to this:</p>
  
  <pre><code>public void setAllComponents(Component[] myComponentArray, Method myMethod) {
      for (Component leaf : myComponentArray) {
          if (leaf instanceof Container) { //recursive call if Container
              Container node = (Container) leaf;
              setAllComponents(node.getComponents(), myMethod);
          } //end if node
          myMethod(leaf);
      } //end looping through components
  }
  </code></pre>
  
  <p>invoked such as:</p>
  
  <pre><code>setAllComponents(this.getComponents(), changeColor());
  setAllComponents(this.getComponents(), changeSize());
  </code></pre>