• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

this keyword

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic