Pages

this keyword in Java

this keyword in Java?

this keyword:
  • this keyword in java is used to represent the current data member variable and current data methods.
  • It is used to initialize values to current data member variables
Demo on this keyword program:
public class ThisDemo {
int a;
String name;
int b = 10;

ThisDemo(int a, String name) {
this.a = a;
this.name = name;
}

public String getA() {
System.out.println("Value of b=" + this.b);
this.printB();
return name;
}

public void printB() {
System.out.println("This is method printB");
}

public static void main(String args[]) {
ThisDemo td = new ThisDemo(10, "This Keyword");
System.out.println(td.getA());
}
}
Output:
Value of b=10
This is method printB
This Keyword
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.