aspose file tools*
The moose likes Beginning Java and the fly likes more than 1 constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "more than 1 constructor?" Watch "more than 1 constructor?" New topic
Author

more than 1 constructor?

Emanuel Mensa
Ranch Hand

Joined: Feb 22, 2013
Posts: 31
can a class have more than 1 constructor? when can i use more than one contructor and why should i use more than on contructor, like this:


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
You can use any number of overloaded constructors, provided they follow the usual rules about overloading. It is worthwhile finding out about the this(); construct for constructor chaining, which you might find here.

Whether you should use multiple constructors is a different matter. I believe you should restrict the constructors to those which definitely establish the class invariant. If you are setting default values like 0 in a constructor, that probably suggests you should delete that constructor. If you are setting default values like null in a constructor, that almost certainly suggests you should delete that constructor.
I also believe every class should have a constructor written by its creator, not a default constructor.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Yes, a class can have as many constructors as you want it to. It is termed as Constructor overloading. When the client(someone who would be using your Student class at his end) wants to create a Student object , he may or may not possess the details of a student. If he has all the details of the student, he will surely call the overloaded constructor. But on case he does not have the necessary details apart from, say, name, then what should he do? Then he would go for the default constructor. He could later on add the instance variable values via his own customized methods. So , in essence, you are making your class flexible by allowing the user to instantiate it even if he does not have the necessary details to populate the instance variables currently.


~ Mansukh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
That is not a default constructor. I would suggest it is dangerous to instantiate an object without knowing the information. If one of the invariants of the student class is index != 0, then all constructors must set up the index.

There is a conceptual problem about having grade as a field of the Student class; the grade might be not known until the student ceases to be a student.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Mansukhdeep Thind wrote:But on case he does not have the necessary details apart from, say, name, then what should he do? Then he would go for the default constructor.

Be careful with terminology. Default constructor has a very specific meaning in Java which is not the meaning you are implying here.

Edit: I must be getting old - Campbell beats me to an answer.

Joanne
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

That is not a default constructor.


Correct Ritchie. Since, Emanuel wrote it in addition to the parametrized one, it is not default constructor. It would have been if the parametrized constructor had not been written and the compiler had provided a no-argument constructor implicitly with same access modifier as the class has as ruled in the JLS:


JLS 8.8.9 Default Constructor

If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided:

if the class is declared public, then the default constructor is implicitly given the access modifier public;
if the class is declared protected, then the default constructor is implicitly given the access modifier protected;
if the class is declared private, then the default constructor is implicitly given the access modifier private;
otherwise, the default constructor has the default access implied by no access modifier.




I would suggest it is dangerous to instantiate an object without knowing the information. If one of the invariants of the student class is index != 0, then all constructors must set up the index.

There is a conceptual problem about having grade as a field of the Student class; the grade might be not known until the student ceases to be a student.


This has more to do with the design of the class rather than the technicalities of constructor overloading. Only when one completely knows the data model / schema design of the proposed application or enhancement, can one comment on what should or should not be represent state of an object.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
I prefer to err on the side of suspicion. If anything can go wrong, assume it will, and permitting a constructor which doesn’t establish the class invariant is an opportunity for somebody to invoke it.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hmm.. Well, may be after 31000 + posts and years of writing Java, my philosophy could be altered.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

Campbell Ritchie wrote:There is a conceptual problem about having grade as a field of the Student class; the grade might be not known until the student ceases to be a student.

that may depend on whether you mean grade in terms of "how well they did in a specific class" vs. "what level of school they are in".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
On this side of the Atlantic it means marks; on your side it might mean type of school. In which case it would be available at the outset of the course.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: more than 1 constructor?