title: Java map, key = class, value = instance of that class
body: <p>I'm not sure what I want to do is possible, but if it is, I want to find out how. Basically, I want to create a Map where the key is a class (<code>java.lang.Class</code>), and value for that entry is an instance of that class. Currently I have</p>
  
  <pre><code>private Map&lt;Class&lt;?&gt;, Object&gt; myMap = new HashMap&lt;Class&lt;?&gt;, Object&gt;();
  </code></pre>
  
  <p>However, this means any Object can be placed in the Map. If it is possible, I want to make it, so only an instance of the class in the key can be placed in the map. Is there any way to use the ? parametrization on the Class to ensure this?</p>
  
  <p>Also, I found there could be a <a href="https://stackoverflow.com/questions/2625546/is-using-the-class-instance-as-a-map-key-a-best-practice">possible memory leak</a> when doing something like this. I'm not sure I fully understand how this happens. I will only be sticking singleton objects into the map, so would there still be concern for a memory leak? If so, how do I prevent it?</p>