Java Programming/Keywords/this
Appearance
this
is a Java keyword. It contains the current object reference.
- Solves ambiguity between instance variables and parameters .
- Used to pass current object as a parameter to another method .
Syntax:
this
.method(); orthis
.variable;
Example #1 for case 1:
public class MyClass
{
//...
private String value;
//...
public void setMemberVar( String value )
{
this.value= value;
}
}
|
Example #2 for case 1:
public class MyClass
{
MyClass(int a, int b) {
System.out.println("int a: " + a);
System.out.println("int b: " + b);
}
MyClass(int a) {
this(a, 0);
}
//...
public static void main(String[] args) {
new MyClass(1, 2);
new MyClass(5);
}
}
|