Home / Programs / this keyword to invoke current class method
🚀 Programming Example

this keyword to invoke current class method

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

💻 Program Code

 class ThisClass{

void method1(){
	System.out.println("it's method1");
	}

	void method2(){
	System.out.println("it's method2");
     //method1();//same as this.method1()
    this.method1();
 }
}

class MainClassofThis{
	public static void main(String args[]){
	ThisClass obj=new ThisClass();
	obj.method2();
	}
}

/*

You may invoke the method of the current class by using the this keyword.
If you don't use the this keyword, compiler automatically
adds this keyword while invoking the method.

*/

                        

🖥 Program Output

it's method2
it's method1
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.