You are here: Home / Topics / Use of this keyword example in Java

Use of this keyword example in Java

Filed under: Java on 2023-09-17 06:56:56

//  Program to use this keyword

class n {

   public int i;

   int getI() {
       return i;

   }

}

class M extends n {

   int a, b;

   void sum(int a, int b) {
       int d = getI();
       this.a = a;
       this.b = b;
       System.out.println("Sum is : "+(d + a + b));
   }

}


public class ThisKeyword{

   public static void main(String[] args) {
       M k = new M();
       k.i = 10;
       k.sum(4, 5);
   }
}

 

Output:

Sum is : 19

About Author:
S
Shyam Dubey     View Profile
If you are good in any field. Just share your knowledge with others.