Here are some examples of how you might use it:
class A{
[B]int value;[/B]
public A([B]int value[/B]){
this.value = value;
}
}
You can see that the constructor has one parameter named value. But the class also has a member variable named value. They both have the same name.
In this situation, the class member will be hidden. In other words, if you are inside the constructor and just refer to value, you are referring to the parameter. In order to refer to the class member, you would need to qualify it with this, as in this.value.
Alternatively, you could have something like this:
class A{
[B]int value;[/B]
public A([B]int val[/B]){
value = val;
}
}
In this case, the class member isn't hidden because the parameter has a different name. So, when you refer to value, you are referring to the class member. And of course when you refer to val you are referring to the parameter. This way is not preferred because you have to give a different name for the parameter and class member and it could get confusing when they are supposed to represent the same thing.
Another use of this is like this:
class B{
public B(){
this(5);
}
public B(int value){
//do something here....
}
}
Here this is used by one constructor to call another constructor within the same class. I believe it has to be the first line in the constructor code.
So if you create a new B object like this:
B test = new B();
The first (parameterless) constructor will be called. The first (and only) line of code in this constructor calls the second constructor which expects an argument of type int.
The reason that one constructor calls another is generally to save yourself from repeating the same code.
For example, consider this class:
class C{
String name;
int age;
public C(){
name = "John";
age = 15;
}
public C(String name, int age){
this.name = name;
this.age = age;
}
}
Alternatively, you could save yourself from having to initialize the member variables more than once by having one constructor call another.
class C{
String name;
int age;
public C(){
this("John", 15);
}
public C(String name, int age){
this.name = name;
this.age = age;
}
}
Creating multiple constructors (or methods) with the same name but different numbers and/or types of parameters is called overloading the constructor (or method). The compiler chooses which one to call based on the number and/or type of arguments.
Finally, let's say you are calling a method that expects one argument of type E:
void someMethod (E obj)
You can pass the current instance of the object from within the class by using this:
class E{
public void test(){
someMethod(this); //When this code is executed, pass the invoking object as an argument
}
}
So if you create two E objects in your code like this:
E first = new E();
E second = new E();
first.test(); //In this case, someMethod will receive a reference to the object referred to by first
second.test(); //In this case, someMethod will receive a reference to the object referred to by second
I hope that clears things up a little bit!