• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using this() keyword in constructors

 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





Can anyone explain to me the point of calling a constructor within a constructor as in the above example? Instead of doing that, why can't the actual constructor just be called? I'm sure there is a reason other than just to give me something else I can't figure out............
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reuse, so you can eliminate code duplication in constructor
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your example, you have three constructors, two with some default
values, and you only had to type this stuff once.

this.x = x;
this.y = y;
this.width = width;
this.height = height;

Of course this is a savings only if you need the three different version.
It also reduces the probability of a transcription error.

Jim ... ...
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition:

Vonique Leary wrote:Can anyone explain to me the point of calling a constructor within a constructor as in the above example?

Well, that would look like we're creating a second new object while we're in the midst of creating one. We need a different syntax to distinguish between (a) creating a new object from scratch, and (b) continuing the already-begun process of creating a new object, by forwarding the call on to some other code (which is itself inside a constructor).
 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, but if you're going to have:



Then why not just call:



Since the one and only thing the first one does is call the second one. Something doesn't make sense to me about that. Why call one thing just to get to the second thing when you can just directly call the first one. But that's just me.
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vonique Leary wrote:
Then why not just call:




I think what you mean is :

=======================


and so you don't need the default non-args constructor
=======================

So, the question comes to why you need the default non-args constructor
- you want to allow someone to instantiate that object using non-args constructor, i.e to have the initial state of the object with initial value (In your example is all 0, but it is not needed in your example since int class property will be auto initialize. Unless you have some object reference that you don't want it to be null when someone instantiate that object using non-args constructor)
- you have a subclass which will be instantiated using subclass's non-args constructor
- you want to use java reflection to instantiate a class (i.e not using "new" keyword)
- you want to use some framework (e.g. Spring, Hibernate) with some functions that required non-args constructor (e.g. Hibernate lazy-loading)


 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that makes more sense now that you have given some reasons for calling a no-arg constructor that refers to another constructor.

Thanks for the explanation!

Von
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vonique Leary wrote:
Since the one and only thing the first one does is call the second one. Something doesn't make sense to me about that. Why call one thing just to get to the second thing when you can just directly call the first one. But that's just me.


that is because reducing the burden to a user/may be a programmer who use your API . he can just concentrate to create an Object insteadof thinking of argument which needs to be passed for constructor or confusing between constructors that which one he need to choose .

<edit>example : new ArrayList() which has its first statement of this(10);</edit>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic