Local class definitions: why does this work
<p>Why does the following style of code work:</p>

<pre><code>BroadcastReceiver receiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        //do something based on the intent's action
    }
}
</code></pre>

<p>I would expect it to be:</p>

<pre><code>private class MyBroadcastReceiver extends BroadcastReceiver () {
    public void onReceive(Context context, Intent intent) {
        //do something based on the intent's action
    }
}

MyBroadcastReceiver receiver = new MyBroadcastReceiver();
</code></pre>

<p>In the 1st code piece above, how does the compiler know that <code>receiver</code> is of type <code>MyBroadcastReceiver</code> and not <code>BroadcastReceiver</code>? Isn't this ambiguous? Why is this allowed?</p>

<p>If I define:</p>

<pre><code>BroadcastReceiver receiver2 = new BroadcastReceiver();
</code></pre>

<p>Now is <code>receiver == reciver2</code>?</p>

<p><strong>EDIT:</strong><br>
BroadcastReceiver 
<a href="http://developer.android.com/reference/android/content/BroadcastReceiver.html" rel="nofollow">http://developer.android.com/reference/android/content/BroadcastReceiver.html</a></p>
