Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is "this" keyword?

 
Ashok Pradhan
Ranch Hand
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the purpose of the "this" keyword?please explain me with example.Thanks..
 
Kevin Cahill
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lukasz Bajzel
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sapika Am
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
purpose : a way to let an object instance refer to itself.
 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Lukasz!

See you guys around.
 
Ashok Pradhan
Ranch Hand
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic