aspose file tools*
The moose likes Beginning Java and the fly likes this keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this keyword" Watch "this keyword" New topic
Author

this keyword

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jan 30, 2000
Posts: 3222
    
    5
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.


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 01, 2000
Posts: 146
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 01, 2000
Posts: 146
Hi Rahul,
Those 2 examples do not yield the same results. Please refer to my explanation above.
Regards,
Milind
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
Consider Paul's rocket mass heater.
 
subject: this keyword