this keyword can be used to return current class instance

Views 👁 3,427

Program


 //Example of this keyword that you return as a statement from the method

class ClassMe{

 ClassMe getClassMe(){
 return this;
 }
void display(){
	System.out.println("Don't confuse");
	}
}

class MainClass{
	public static void main(String args[]){
	new ClassMe().getClassMe().display();
	}
}

/*
<b>this keyword can be used to return current class instance:</b>

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).

*/

Output

Don't confuse
Press any key to continue . . .