File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this keyword

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
i am not able to understand the use of 'this keyword'. i know that it is used to refer to the current class object..why 'this' is used? why not create class's object and use it??give example.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this: refers to the current object while super refers to the superclass method or variable.
<PRE>public class Test {
int i,j;
Test(int i, int j) {
this.i = i;
this.j = j;
}
}</PRE>
Is equivalent to:
<PRE>public class Test {
int i,j;
Test(int i, int j) {
i = i;
j = j;
}
}</PRE>
but it's less clear.
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
hi tony,
the two examples that u have given are not equal or same i put a program to demonstrate this. i is a local variable in the constructor and thus does not refer to the static variable i. while j prints out 4 correctly.

public class Test
{ static int i,j;
Test(int i, int j) {
i = i; this.j = j;
}
public static void main(String args[]){
Test e=new Test(4,4);
System.out.println(i);
System.out.println(j);
}
}
// i prints out 0 not 4;
monica,
since the two examples print out different results. the importance of this is clarified.

Rahul

[This message has been edited by rahul_mkar (edited June 11, 2000).]
 
Milind Kulkarni
Ranch Hand
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul,
Please refer to the program given below:
public class Test
{ static int i,j;
Test(int i, int j) {
i = i; this.j = j;
}
public static void main(String args[]){
Test e=new Test(4,4);
System.out.println(i);
System.out.println(j);
}
}
Here the class Test has two member variables: i & j. Constructor accepts 2 arguments which are also sharing the their names with the member variables for which the arguments provide an initial values.
The argument names here hide the member variables. So the values i and j refer to the argument in the body of the constructor, not to the member varibales. To access member variables a prefix this. must be used.
Now coming to the example given above. In the constructor the value of i (member variable) is not changed to 4 because you are assigning the value of the argument (i) to itself and not to the member variables. The value of j is changed correctly because of the following assignment.
this.j = j;
Hope this helps !!
Regards,
Milind

[This message has been edited by Milind Kulkarni (edited June 13, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
hi,
milind what u r saying is what i am saying. However i am contradicting tony's two examples. that they are not the same by giving the example i gave above. whether i and j are static or instance variables does not matter IMO. please refer tony's two examples and check out if they are the same as tony says it to be.
regds.
Rahul
 
Milind Kulkarni
Ranch Hand
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul,
Those 2 examples do not yield the same results. Please refer to my explanation above.
Regards,
Milind
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
hi milind,
i know that and that is why i replid in this thread that tony is wrong.

please read what i have written in BOLD earlier.
Rahul
[This message has been edited by rahul_mkar (edited June 14, 2000).]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice demonstration of this but Monika wanted to know why you can't just use the object reference. The answer is that at the moment you are using "this" you don't have an object reference. In rahul_mkar's example, the constructor has no way to get an object reference to itself. So "this" provides that reference.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic