Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The keyword 'this'

 
cheng jian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
Now i'm puzzled by the keyword 'this'.
Is anyone help me?
As detailed as possible!With some examples are best!
Thank you!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by cheng jian:
With some examples are best!


Try THis and let me know


Hope This Helps
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java this is keyword , whcih points the current object which invokes instance methods !
Like , here is an example



'this.a' belongs to object which call the constructor , whereas right side 'a' is method local variable .

that`s used to resolve the ambiguity . If you remove keyword this, you cant assigned the values to instance variable ! Try this,



and SUN explains it beautifully !
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use this(); as the first line in an overloaded constructor to call another overloaded constructor of the same class. Obviously you can have arguments in the ().

If you are inside an inner class and want a reference to the object of the outer class associated with it, write

OuterClassName.this.foo();
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. And thanks to everybody else who replied.
 
cheng jian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
I think i get something from the examples.
Here i learn many.
Thank you again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic