title: How to write a Java constructor for variable number of arguments of different types?
body: <p>I have to write a constructor for a class Stamp. The constructor is supposed to take <strong>up to five</strong> arguments of types String, int, and double. So, the constructor would look like this:</p>
  
  <pre><code>public Stamp(String code, int year, String country, double value, int numberOfCopies){...}
  </code></pre>
  
  <p>The problem is, as the object of the class Stamp is being created, not all the parameters may be provided, i.e., an object can be declared as</p>
  
  <pre><code>Stamp stamp = new Stamp("some_code", 1991, "Burkina Faso", 50, 1000);
  </code></pre>
  
  <p>as well as</p>
  
  <pre><code>Stamp stamp = new Stamp("some_code", 1991, "Burkina Faso");
  </code></pre>
  
  <p>and the constructor has to work in both cases, even if the list of arguments is truncated (in the latter case, some default values are assigned to <em>value</em> and <em>numberOfCopies</em>). Of course, I can just write six constructors (for the possible number of parameters from 0 to 5, assuming that the parameters always follow the order described above, without mixing up), but there should be a smarter way. I know I can probably declare the constructor like</p>
  
  <pre><code>public Stamp(Object[] params){...}
  </code></pre>
  
  <p>and then cast the elements of params into corresponding classes. This will probably work but I will always have to check how many parameters are provided for the constructor using "if" conditions in order to decide whether to assign default values to variables if the corresponding parameters are not provided or use the provided values if those are given. This all seems pretty ugly. Thus, the question is brief: what is the way to build a constructor (or some other method) if the list of provided parameters varies in length and the types of parameters are different?</p>