title: generic container of a generic class in java
body: <p>What should I write for "self" in Java? What I want "self" to mean here is "whatever class this concrete implementation of Vehicle is". Is that possible in Java?</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  abstract class Vehicle {
      private Garage&lt;self&gt; garage;
  
      void setGarage(Garage&lt;self&gt; g) {
          this.garage = g;
          g.vehicles.add(this);
      }
  }
  
  class Car extends Vehicle {
  }
  
  class Garage&lt;T extends Vehicle&gt; {
      List&lt;T&gt; vehicles = new ArrayList&lt;&gt;();
  
      void addVehicle(T t) {
          vehicles.add(t);
      }
  }
  
  public class GenericFoo {
      public static void main(String[] args) {
          Garage&lt;Car&gt; carGarage = new Garage&lt;&gt;();
  
          Car c = new Car();
          carGarage.addVehicle(c);
          c.setGarage(carGarage);
      }
  }
  </code></pre>