Home / Programs / this keyword can be used to return current class instance
🚀 Programming Example

this keyword can be used to return current class instance

👁 3,427 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

💻 Program Code

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

*/
                        

🖥 Program Output

Don't confuse
Press any key to continue . . .
                            
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.