• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in this()

 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What is the main use of this(); and this.a = a.
Why we are using this. I referred in some books, it's simply saying it's points to the current object. Could u pls explain with one simple example.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's used a lot in constructors, to differentiate between the parameter passed in and the class variable with the same name... i.e.


note that without the keyword "this", the compiler can't tell which param1 you are talking about.
 
Ryan McGuire
Ranch Hand
Posts: 1078
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Niyas Ahmed Sheikh:
Hi,

What is the main use of this(); and this.a = a.
Why we are using this. I referred in some books, it's simply saying it's points to the current object. Could u pls explain with one simple example.




The this() in line 13 calls the no-argument constructor.
Line 14 sets the instance field called age to the value of the passed in argument called age. The this is required to distinguish the instance field from the method argument of the same name.

Make sense?

Ryan
 
ganesh pol
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi my dear friend
you can refer this particualr site
http://www.developer.com/java/article.php/1440571

or

http://java.sun.com/docs/books/tutorial/java/javaOO/thiskey.html
 
Kumar Sub
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use it for member help in JBuilder. When editing code, I forget the member variable's name, it is easier to type in 'this.', select a member and continue. Helps you to continue coding and also avoid typing errors.

Then to standardize you might want to leave the 'this.' in all member references or get rid of it completely. I try to stay away from using member names for parameters, but that also happens sometimes and the 'this.' remains until I go back and clean.
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i try to always preface instance variables and instance methods (members) with "this", to make it explicit that i'm calling something specific to the current object. explicit is better than implicit.
 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ryan McGuire:


The this() in line 13 calls the no-argument constructor.
Line 14 sets the instance field called age to the value of the passed in argument called age. The this is required to distinguish the instance field from the method argument of the same name.

Make sense?

Ryan


So your are telling the main use of "this" is distinguish the instance variable from the method argument. Correct me If i'm wrong, instead of assigning "age" to one new variable(x), we can use this operator.

12 public Ryan(int age) {
13 this(); // Do the default initialization.
14 x = age;
15 }

Another doubt in this(); Whether we can have any argument in the this();
this() is mainly to call constructor alone?
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can pass an argument using this() - as in this(x, y, z). For example:



Although not really a useful example - it does demonstrate the use of this(). And now I have had enough of that() ;-)

Regards,
JD
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
note that there is a difference between


and


the first is saying "use the 'myVar' that is a member variable of the class i'm currently in".

the latter is saying "call the constructor for the class i'm currently in that takes a single parameter of whatever type myVar is".
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niyas,

Just a couple of points I forgot to make in my previous post:

1) this() can only be used from another constructor.
2) this() must be the first line in the constructor.

Regards,
JD
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic