title: Java generic GetComponent method
body: <p>I am trying to make a java component system similar to that of the UnityEngine and wanted to build a generic GetComponent() method. I am stuck when trying to return the type found</p>
  
  <pre><code>    public &lt;T&gt; T GetComponent (){
      for (Component c: this.components){
      //I know this doesn't work, I want to know how to check if the type 
      //matches and return it (ie. Collider col = GetComponent&lt;Collider&gt;();)
          if(c == T)){
              System.out.println("Component of type: " + c.name + " found!");
              return T;
          }
      }
      System.out.println("No component found");
      return null;
  }
  </code></pre>
  
  <h1>Edit</h1>
  
  <p>Thanks to all who answered. This has been a learning experience in just how much Java and C# differ.</p>