For example, if I have this:
class Test{
private int id;
public Test(int id){
id=id;
}
}
In this case, how can I assign the value of the id parameter to the id field?
Use
this.id = id;
because this refers to the current object.
Learn more here: http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
id shadows the instance field id.Use this:
public Test(int id){
this.id = id;
}
From here:
Within an instance method or a constructor,
thisis a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by usingthis.
this.id=idthismeans the current instance