Return a private class implementing an interface keeping the implementation private
<p>Static method <code>M</code> returns an object implementing interface <code>A</code>:</p>

<pre><code>interface A { ... }
static A M() { ... }
</code></pre>

<p>Within <code>M</code> I would like to construct an object of type <code>B</code> and return that, given that <code>B</code> implements <code>A</code>:</p>

<pre><code>class B implements A { ... }
</code></pre>

<p>I do not want client code to know anything about how <code>B</code> is implemented, I would <em>prefer</em> for <code>B</code> not to be a <code>static</code> class, <code>B</code> <em>must</em> be immutable and there could be different <code>B</code> handed to different clients. I want to prevent instantiation of <code>B</code> outside method <code>M</code> at all costs (short of reflection, as one user commented).</p>

<p>How can I achieve the above? Where and how should I implement <code>B</code>? Could you please provide a short code example?</p>

<p>My main problem is: how can I have "different <code>B</code>s?"</p>
