wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What is Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is "this" keyword?" Watch "What is "this" keyword?" New topic
Author

What is "this" keyword?

Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

What is the purpose of the "this" keyword?please explain me with example.Thanks..
Kevin Cahill
Greenhorn

Joined: Jan 08, 2008
Posts: 9
http://java.sun.com/docs/books/tutorial/java/javaOO/thiskey.html

There are a number of these tutorials at this site which I've been finding very useful in learning basic Java stuff. Sure wish they had these things when I learned C 15 years ago.


Persistence and determination alone are omnipotent.<br />---Calvin Coolidge
Lukasz Bajzel
Greenhorn

Joined: Dec 03, 2007
Posts: 26
"this" refers to the current object you are working with. Within a given object you can use "this" keyword to refer other members in the class.

Hope this helps!

Sincerly,
Your friends at www.javaadvice.com
www.javaadvice.com - The one stop resource for all your Java questions and answers.
Ammar Salem
Ranch Hand

Joined: Dec 29, 2007
Posts: 60
Two uses of "this" keyword are
1) To resolve the ambiguity between instance variables and parameters.
2) To pass the current object as a parameter to another method.

consider this code:

the code provides a class definition that demonstrates these uses.The MyDate class declares instance variables, (lines 2-4). One of the parameters to one of the constructors(line 6-10) is also called day,so in that context the keyword this resolves the ambiguity(line 7).The addDays method creates a new date object(line 18).IN THIS CONSTRUCTOR CALL,the method uses this keyword as an argument to refer to the current object.
please note the line codes which has a * .I have numbered the code for you for more understanding.
thank you
Ammar Salem


[ January 17, 2008: Message edited by: Ammar Salem ]
[ January 17, 2008: Message edited by: Ammar Salem ]

****************************<br />For Some Dreams.....<br />One Lifetime Is Not Enough.<br />****************************
Sapika Am
Greenhorn

Joined: Jan 15, 2008
Posts: 10
purpose : a way to let an object instance refer to itself.
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Hello folks!
I'm studying the same thing and I'm finding some difficulties in using this in a constructor.
Like... the sun's example (rectangle)( http://java.sun.com/docs/books/tutorial/java/javaOO/thiskey.html )



If I make a new Rectangle(20, 20), it would first go into constructor 1 and then, with the this keyword go into the constructor 2. Is that right?

Thanks once more folks!
[ January 18, 2008: Message edited by: Andre Brito ]
Lukasz Bajzel
Greenhorn

Joined: Dec 03, 2007
Posts: 26
You defined 3 constructors here.
1. No parms
2. 2 parms
3. 3 Parms

So when you create an instance of the class by calling new Rectangle(), the constructor with no parm (1) gets invoked. When you use new Rectangle(20,20) the second contructor only gets called. No other constructors are invoked. Similarly with the 3 parm option, the third one will get called.

Now since you have this(0, 0, 0, 0); code in the costructor 1, a call new Rectangle() will eventually cause the cotrol to go to constructor 3. Similar is the case with constructor 2 . But note that only the new Rectangle() call goes to constructor 1. When you use new Rectangle(20,20), it control goes to constructor 2 but will never go to constructor 1.


Hope this helps!

Sincerly,
Your friends at www.javaadvice.com
www.javaadvice.com - The place where your questions are answered directly.
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Lukasz,

So, both of my constructor (the first one and the second one) calls the constructor that has 4 arguments? (Because of keyword "this")?
Lukasz Bajzel
Greenhorn

Joined: Dec 03, 2007
Posts: 26
Yes. Both of them calls the constructor with 4 arguments because you have a call like "this(x,y,z,q);". But note that once that call is executed, the control gets back to the original contructor and continue executing the statements there.

We think you got the concept here. Keep exploring..

Sincerly,
Your friends at www.javaadvice.com
www.javaadvice.com - The one stop resource for all your Java questions and answers.
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Thanks, Lukasz!

See you guys around.
Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

Thanks everyone !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is "this" keyword?