aspose file tools*
The moose likes Beginning Java and the fly likes Using this() keyword in constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using this() keyword in constructors" Watch "Using this() keyword in constructors" New topic
Author

Using this() keyword in constructors

Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107





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

Joined: Jan 23, 2008
Posts: 210
reuse, so you can eliminate code duplication in constructor


SCJP 6, SCWCD 5, SCBCD 5
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2970
    
    9
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

Joined: Mar 24, 2008
Posts: 107
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

Joined: Jan 23, 2008
Posts: 210
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

Joined: Mar 24, 2008
Posts: 107
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

Joined: Jan 28, 2008
Posts: 5575

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>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using this() keyword in constructors
 
Similar Threads
whats the Point?
newbie's question about constructor
Autoscrolling for marquee selection
Trying to get values for the perimeter and center of a rectangle.
What is "this" keyword?