A theorem in Sakurai's QM book (section 1.4) I was trying to understand the theorem 1.2 in Sakurai's Modern QM, it is on page 29 (the second edition):

Suppose that $A$ and $B$ are compatible observables, and the eigenvalues of $A$ are nondegenerate. Then the matrix elements $\langle a'' | B | a' \rangle$ are all diagonal.

I understand the proof for the case of $a'' \neq a'$ in the sense that if $a'' \neq a'$ then $\langle a'' | B | a' \rangle = 0$. But I am not sure what will happen if the eigenvalues of $A$ are degenerate. I think it is somehow related to the case when $a'' = a'$.
 A: 
I was trying to understand the theorem 1.2 in Sakurai's Modern QM, it is on page 29 (the second edition):

Suppose that $A$ and $B$ are compatible observables, and the eigenvalues of $A$ are nondegenerate. Then the matrix elements $\langle a'' | B | a' \rangle$ are all diagonal.

I understand the proof for the case of $a'' \neq a'$ in the sense that if $a'' \neq a'$ then $\langle a'' | B | a' \rangle = 0$. But I am not sure what will happen if the eigenvalues of $A$ are degenerate. I think it is somehow related to the case when $a'' = a'$.

In the case where $A$ has degenerate eigenvalues, then we need a second label for our states. Only in the case where $A$ is non-degenerate does it make sense to label the states with $a$ alone. Rather we should now (in the degenerate case) write:
$$
\hat A |a,m\rangle = a|a,m\rangle\;,
$$
where $m$ is a new label that accounts for the degeneracy.
We can now use the fact that $[A, B]=0$ in exactly the same way as the degenerate case to find:
$$
\left(a - a'\right)\langle a,m|B|a',m'\rangle = 0\;,
$$
or rather:
$$
\langle a,m|B|a',m'\rangle 
= \delta_{a,a'}\langle a,m|B|a,m'\rangle
\equiv \delta_{a,a'}f_a(m,m')\;,
$$
where $f_a(m,m')$ is a function of $a$, $m$, and $m'$, and is not generally diagonal in $m$ and $m'$. (However, we can use the fact that $B$ is an observable, so we know $B=B^\dagger$, to show that $f_a^*(m,m')=f_a(m',m)$.)
So, how do we say this in words? We say that $B$ is block diagonal because $B$ still can not connect the subspaces with different $a$ values.
As a concrete example, consider a 4x4 matrix $A$ in the basis where the $|a,m\rangle$ are eigenvectors of $A$. In this example both eigenvalues are assumed to be doubly degenerate.
In this basis, $A$ looks like:
$$
A = \left(\begin{matrix}a_1 & 0 & 0 & 0 \\ 0 & a_1 & 0 & 0 \\ 0 & 0 & a_2 & 0 \\
0 & 0 & 0 & a_2 \end{matrix}\right)
$$
and $B$ looks like:
$$
B = \left(\begin{matrix}b_1 & b_2 & 0 & 0 \\ b_2^* & b_3 & 0 & 0 \\ 0 & 0 & b_4 & b_5 \\
0 & 0 & b_5^* & b_6 \end{matrix}\right)
$$
