Overlays with Code Listings

You cannot include a lstlisting (package listings) in a only or visible command in LaTeX beamer. BUT you can define the listing beforehand and then include that inside the only or visible!

Example (from slides about recursion in Java):

\defverbatim{\Lst}{
\begin{lstlisting}
public int fakultaet(int n) {
   if ( n == 1 ) {
      return 1;
   } else {
      return n * fakultaet( n-1 ) ;
   }
}
\end{lstlisting}
}

\begin{frame}[fragile]
\frametitle{Aufgabe: Fakult├Ąt von $n$}

Definition:
\begin{itemize}
   \item \lstinline{fakultaet( 1 ) = 1}
   \item \lstinline{fakultaet( n ) = n * fakultaet( n-1 ) }
\end{itemize}

\bigskip

Java Code: 
\visible<2|handout:0>{\Lst}

\end{frame}