aspose file tools*
The moose likes Beginning Java and the fly likes 2 Construtors for 1 class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "2 Construtors for 1 class?" Watch "2 Construtors for 1 class?" New topic
Author

2 Construtors for 1 class?

Thomas Knight
Ranch Hand

Joined: Feb 08, 2003
Posts: 33
Forgive my question, I am new to Object Oriented programming and Java, but not to programming. I am having a little trouble with the constructor concept. A constructor is a method with the same name as the class, correct? It is what really creates the class, correct?
I was looking at an example where a class had 2 different constructors, each was different in the parameter list by having different types and numbers of parameters. How can this be, I don't understand this can someone explain why people do this, what is it used for? Thanks
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Thomas,
i'd rely on somebody else for detailed explanation and may be a better one but i've a simple practical example...
imagine that ur mom told u to get something from a grocery store. now, there r two possibilities,
scenario-1: she doesn't tell which grocery store to go
scenario-2: she tell us to go to 'x' grocery store
now the 1st scenario makes u use ur common sense. u will go to the store which is,
1. near by,
2. u already know.
(both these cases assumes that u 'll only think about stores that can have the grocery item being asked. obvious enough).
in 2nd scenario implies that u 'll first try the store u r told (assuming ur in good state of mind to follow mom's advice.. )
(We will ignore the case that will make u go another store if this first store person says-we don't have it)
as u observe- in both the scenario u r ONLY ONE. u follow things based upon the input given to u. in 1st scenario u assumed some "default" values so to say ...(that is store to go)
and in 2nd scenario u got input about the store to go from the external source (ur mom in this case)...
in any scenario u accomplish the task of getting the specified thing from the grocery store, right???
same with OBjects in java (or any other OO). u can have two constructors for the same object as sometimes u want to use "default" values to instantiate the object and sometimes u want to be able to specify "ur own values" to the object initialization...
u can have any number of constructors u want to have..
i hope i am helpful in little way...
regards
maulin....
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
technically speaking,
its better to have multiple constructors sometimes to avoid "code duplication" which is essentially the concept of "code maintenance".
e.g. u had a main constructor (that u wrote first) that had initialization for parameters to its default values. say u have 5 parameters.
now, in ur application u want to allow other objects to interface with this object and let other objects specify certain input at the object creation time. u create another constructor having 2 input parameters as u wanted to allow other objects to specify two inputs out of 5.
still u have to make other 3 parameters initialized to default value as u do in first constructor , right?
so, there r two options now,
1. u duplicate the initiatilization of the other three parameter to their default value in the 2nd constructor
2. u call this() instead in the second constructor.
which is better???
of course 2nd. because if in future u want to change default values for parameters to something else then u have to just change the 1st constructor but if u did follow the 1st option then u'd endup changing both of the constructors, right??? which is liable to errors and bugs when the software grows in size...
i'm sorry that i'm wordy. instead a simple example would be better but i'm poor at that..
regards
maulin.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
A trivial example:

This class has two constructors, the second one is suplied more as a convenience so you do not have to do new Complex( x, 0 ); when you have only the real part. new Complex( x ); does the trick.
Note that constructors are not methods. The creation of such constructors is an example of Constructor Overloading
[ March 28, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Well, I would argue that constructors ARE methods, just specialized ones that are called in a special way (i.e. with the new keyword). Of course, I think this is just splitting hairs anyway.
However, constructor overloading is very similar to method overloading, so you may want to look into this broader topic to help you understand the more specific example of constructor overloading.
HTH
Layne


Java API Documentation
The Java Tutorial
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi all,
i would not like to divert the discussion but i agree 100% that constructors are methods with special convention/restrictions (if u will)...

regards
maulin.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

I was looking at an example where a class had 2 different constructors, each was different in the parameter list by having different types and numbers of parameters. How can this be, I don't understand this can someone explain why people do this, what is it used for?

Constructor creates and initializes an object. If you want to provide different means of object initialization (as a convinience to the user of the class), overload the constructor. Simple as that.
Eugene.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Maulin Vasavada:
i agree 100% that constructors are methods with special convention/restrictions (if u will)...

I believe that it is because of all the "special convention/restrictions" one can say that constructors are not methods. The JLS also distinguishes between methods and constructors in Section 8, and uses the terms "methods or constructors" or "methods and constructors" throughout. I also found that keeping in mind the idea that "constructors are not methods" got me more than a few points when taking the SCJP recently.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Barry Gaunt:

I believe that it is because of all the "special convention/restrictions" one can say that constructors are not methods. The JLS also distinguishes between methods and constructors in Section 8, and uses the terms "methods or constructors" or "methods and constructors" throughout. I also found that keeping in mind the idea that "constructors are not methods" got me more than a few points when taking the SCJP recently.

All righty then! I guess I should change my way of thinking. Up until now, I have considered constructors as methods, except... (insert any appropriate special conditions here). I can see how these exceptions can lead to the conclusion that constructors aren't methods. Thank you for pointing this out.
Thomas Knight
Ranch Hand

Joined: Feb 08, 2003
Posts: 33
Wow thanks for all the replys guys. It will take me sometime to read through all of them and digest them
 
Don't get me started about those stupid light bulbs.
 
subject: 2 Construtors for 1 class?