title: Java - Multiple constructors using same data type
body: <p>How can we create constructor, to set different values that are from same data type?   </p>
  
  <p>Its impossible two create two identical constructors:</p>
  
  <pre><code>public User(int age, String name){
  }  
  public User(int age, String surname){
  }
  </code></pre>
  
  <p>User user1 = new User(33, <strong>Jack</strong>);<br>
  User user2 = new User(33, <strong>Sparrow</strong>);</p>
  
  <p>Since both name and surname are from same data type, its impossible to know which value the user has meant to set.  </p>
  
  <hr>
  
  <p>We can create a constructor that has all the properties and then pass null for unset arguments.</p>
  
  <pre><code>public User(int age, String name, String surname){
  }  
  </code></pre>
  
  <p>User user1 = new User(33, <strong>Jack</strong>);<br>
  User user2 = new User(33, null, <strong>Sparrow</strong>); </p>
  
  <hr>
  
  <p>There is a way to make it, using HashMap imitating the javascript object literal.</p>
  
  <pre><code>package javaapplication;
  
  import java.awt.Color; 
  import java.util.HashMap;
  
  public class JavaApplication {
  
      public static class User {
  
          public int age;
          public String name;
          public String surname;
          public Color hairColor;
  
          public User(HashMap&lt;String, Object&gt; arguments) {
  
              if (arguments.containsKey("hairColor")) {
                  this.hairColor = (Color) arguments.get("hairColor");
              }
  
              if (arguments.containsKey("name")) {
                  this.name = (String) arguments.get("name");
              }
  
              if (arguments.containsKey("surname")) {
                  this.surname = (String) arguments.get("surname");
              }
  
              if (arguments.containsKey("age")) {
                  this.age = (int) arguments.get("age");
              }
          }
      }
  
      public static void main(String[] args) {
  
          User jack1 = new User(new HashMap&lt;String, Object&gt;() {
              {
                  put("hairColor", Color.RED);
                  put("name", "Jack");
                  put("age", 33);
              }
          });
          System.out.println(jack1.hairColor);  // java.awt.Color[r=255,g=0,b=0]
          System.out.println(jack1.surname);    // null
          System.out.println(jack1.name);       // Jack
          System.out.println(jack1.age);        // 33
  
          User jack2 = new User(new HashMap&lt;String, Object&gt;() {
              {
                  put("hairColor", Color.RED);
                  put("surname", "Sparrow");
                  put("age", 33);
              }
          });
          System.out.println(jack2.hairColor);    // java.awt.Color[r=255,g=0,b=0]
          System.out.println(jack2.surname);      // Sparrow
          System.out.println(jack2.name);         // null
          System.out.println(jack2.age);          // 33
  
      }
  
  }
  </code></pre>
  
  <p>Is there a more elegant way to do it?</p>