title: Java: Passing classes as parameters at runtime
body: <p>I'm writing a sort of game engine. All of my objects extend from a <code>GameObject</code> class. I need to be able to check whether an object is touching another object, specifying the type. My code is thus:</p>
  
  <pre><code>public boolean collidesWith(GameObject caller, Class collideType) {
      for( /* the array of all GameObjects */ ) {
  
          // only check against objects of type collideType
          // this line says "cannot find symbol: collideType"
          if(gameObjects[i] instanceof collideType) {
              // continue doing collision checks
              // return true in here somewhere
          }
          else continue;
      }
      return false;
  }
  </code></pre>
  
  <p>What I can't understand is how to pass something like <code>BouncyBall</code> to <code>collidesWith()</code>. Ideally I don't want to have to create an instance for each call of <code>collidesWith()</code>, though if I absolutely must, I can work with it.</p>
  
  <p>Tons of questions and answers here have dealt with silly things like:</p>
  
  <ul>
  <li>whether it's appropriate to use <code>instanceof</code></li>
  <li>whether I want to be passing a class</li>
  <li>whether I <em>really</em> want to be passing a class</li>
  </ul>
  
  <p>Do I need to use reflection? Must I get the class's name and compare it with <code>equals()</code>? Is creating an instance necessary?</p>