To return current class instance

We can return this keyword as an statement from the method. In such case, return type of the method must be the class type (non-primitive). Let's see the example:


\begin{lstlisting}
class A{
A getA(){
return this;
}
void msg(){System.o...
...tatic void main(String args[]){
new A().getA().msg();
}
}
\end{lstlisting}

Output:


\begin{lstlisting}
Hello java
\end{lstlisting}