• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor parameters  RSS feed

 
Marshall Brett
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure there is an easy solution to this, but I'm not sure how to phrase my question to make it search engine friendly. I'm just learning how to use constructors. I've made a class with 5 properties and a default constructor. However, I'm trying to figure out how to allow object creation even if not all the properties are passed. For example:

(in main class)


(in Dinosaurs class)


Is there a way to pass 1 of the 5 properties (and have it assigned to the right variable) without defining the others, say, if it was read as input or passed from the command line at execution (without making 5-factorial variations of the constructor)? Is there a more effective way to assign properties to an object?
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Welcome to the Ranch
How about using the "this" keyword
 
Marshall Brett
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this() is in the constructor with all 5 properties. However, if you try to pass a single property as shown you get an error.
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
off course you will get an error because the compiler is looking for a constructor signature with five parameters and you are passing it just one parameter, that's why the compiler complains

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marshall Brett wrote:this() is in the constructor with all 5 properties. However, if you try to pass a single property as shown you get an error.



No black magic here... if you have 5-factorial ways to call your constructor, and you would like your calling code to actually do that, then you will need 5 factorial overloaded constructors.

However, as mentioned, since constructors can call constructors, there is no reason to implement it 5 factorial times. For example, you can just implement the version that takes 5 parameters -- and as for the other overload constructors, they can just call the 5 parameter version, passing reasonable defaults for the parameters that it doesn't have.

Henry
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java doesn't have 'optional' parameters, like some other languages. So to do what you need to do, you need to do some work arounds. One common one is to use a factory and/or config class(es) that store good default values for properties, and allow you to assign just the ones you know or want to override. You then use the config class to build the Object of interest. Example, your dinosaur:

I will add a DinosaurBuilder class, which has an overloaded factory method used to build Dinosaurs:


Not much better than you problem with having many constructors. So the second part of it is to have a config class that can be used to store values and works as an intermediate between the builder and object. So you might do this:

In this scenario, you have a DinosaurConfig object that can store default values, and you can modify the values, one at a time, to generate the configuration you need. You don't need to have lots of createDinosaur() methods (or constructors) because the caller could use the DinosaurConfig class to build things as he likes:

But providing some convenience methods to cover the normal cases (like the createDinosaur(String) and createDinosaur(String, String) methods) might make the system easier to use without an excess of overhead.

Finally, I have also seen it where the Config class has all the properties set using methods which return the Config object itself. This lets you chain the calls together and make a complete Dinosaur in basically one line of code:

Which could be used like:
 
Mauro Trevigno
Ranch Hand
Posts: 99
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marshall,

Same as methods, constructors could be overloaded, means you can define several constructors changing the order, quantity and type of parameters they get.

After that, depending the way you create the object with the word *new* you need to choose the constructor that works for you, based on the parameter list.

Ex.




Hope this helps.

Regards,
Mauro.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!