title: How to avoid creating the same object reference multiple times?
body: <p>Imagine that I have some classes that looks like this:</p>
  
  <pre><code>class Car {
      private Image carImage;
  
      public Car(int imageIndex) {
          switch (imageIndex) {
              case 1: carImage = generateCarImage(1); break;
              # and so forth
          }
      }
  }
  
  class Audi extends Car {
      private int numberOfSeats;
      public Audi(int imageIndex, int numberOfSeats) {
          super(imageIndex);
          this.numberOfSeats = numberOfSeats;
      }
  }
  </code></pre>
  
  <p>Now imagine that I create multiple Audi's using the same image:</p>
  
  <pre><code>Audi car1 = new Audi(1,2);
  Audi car2 = new Audi(1,3);
  </code></pre>
  
  <p>Will car1 and car2 extend the same object? I assume not, but is there a way I can make it so? I'm asking because I want to avoid generating and storing the same image twice.</p>
  
  <p>EDIT:</p>
  
  <p>Will these two audi's reference the same car, e.g. the image is generated and stored only once, and any changes to one affects the other? </p>
  
  <pre><code>class Car {
      private Image carImage;
      public Car(int imageIndex) {
          switch (imageIndex) {
              case 1: # carImage = readfile(1.jpeg)
              # and so forth
          }
      }
  }
  
  class Audi{
      private int numberOfSeats;
      private Car car;
      public Audi(Car car, int numberOfSeats) {
          this.car = car;
          this.numberOfSeats = numberOfSeats;
      }
  }
  
  Car car = new Car(1);
  Audi audi1 = new Audi(car,2);
  Audi audi2 = new Audi(car,2);
  </code></pre>
  
  <p>EDIT 2:</p>
  
  <p>There are a lot of good answers here, and I ended up using a combination of them to create a decent solution. My initial problem was not very well defined, mainly because I didn't know myself exactly what it was.</p>
  
  <p>Anyway, for this problem it is not possible to generate all the data (<code>PartsInfo</code> in the example below) beforehand, nor can I generate the data explicitly (as implied by the switch-case example above). The biggest problem with the solution below is that I can't access individual fields in <code>PartsInfo</code> without retrieving the whole thing (as is done in the solution when Car.getPartsInfo() is called) or creating multiple instances of the same object (in which case the Car class would get its own PartsInfo variable).</p>
  
  <p>A weak hashmap would also do, but not optimal because the problem is not garbage collection, but huge amount of identical data stored in separate instances. </p>
  
  <p>The solution is applicable if the ID is something like "audi-a4-2003" and <code>PartsInfo</code> is identical for all "audi-a4-2003" independent of color, owner, age, number of seats etc, but completely different for "audi-a4-2004". </p>
  
  <p>Thanks    </p>
  
  <pre><code>Class PartsInfo {
      // lots of stuff I'd rather not create nor save multiple times
  }
  
  Class PartsInfoFactory {
      private static HashMap&lt;String, PartsInfo&gt; partsInfoMap = new HashMap&lt;String, PartsInfo&gt;();
  
      public static getPartsInfo(String id) {
          if (!partsInfoMap.containsKey(id)) {
              generatePartsInfo(id);
          }
          return partsInfoMap(id)
      }
  
      private static generatePartsInfo(String id) {
          // Do stuff I don't want to do twice for same ID
          partsInfoMap.put(id)
      }
  }
  
  Class Car {
      private Color color;
      private String id;
      // Notice that PartsInfo is not stored here
  
      public Car(Color color, String id) {
          this.color = color;
          this.id = id;
      }
  
      public PartsInfo getPartsInfo() {
          return PartsInfoFactory.getPartsInfo(id);
      }
  }
  </code></pre>