this keyword to invoke current class method

Java Programming Language Class, Object and Methods in java (Article) Class, Object and Methods in java (Program)

3140

Program:

 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.

*/

Output:

it's method2
it's method1
Press any key to continue . . .

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.