411 REFERENCE · Wikibooks
Java Programming/Keywords/this
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
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:
.method();
or
.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);
}
}
deep