File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Doubt in this() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in this()" Watch "Doubt in this()" New topic
Author

Doubt in this()

Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
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

Joined: Oct 02, 2003
Posts: 11231
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1007
    
    3
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

Joined: Apr 29, 2005
Posts: 151
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

Joined: Jun 15, 2005
Posts: 1
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

Joined: Jan 14, 2005
Posts: 323
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

Joined: Jun 15, 2005
Posts: 129
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

Joined: Apr 08, 2004
Posts: 130
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

Joined: Oct 02, 2003
Posts: 11231
    
  16

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

Joined: Apr 08, 2004
Posts: 130
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in this()