title: Proper way to declare and set a private final member variable from the constructor in Java?
body: <p>There are different ways to set a member variable from the constructor. I am actually debating how to properly set a final member variable, specifically a map which is loaded with entries by a helper class.</p>
  
  <pre><code>public class Base {
      private final Map&lt;String, Command&gt; availableCommands;
      public Base() {
          availableCommands = Helper.loadCommands();  
      }
  }
  </code></pre>
  
  <p>In the above example the helper class looks like this:</p>
  
  <pre><code>public class Helper {
      public static Map&lt;String, Command&gt; loadCommands() {
          Map&lt;String, Command&gt; commands = new HashMap&lt;String, Command&gt;();
          commands.put("A", new CommandA());
          commands.put("B", new CommandB());
          commands.put("C", new CommandC());
  
          return commands;
      }
  }
  </code></pre>
  
  <p>My thought is, that is better practice to use a method to set such a variable in the constructor. So Base class would look something like this:</p>
  
  <pre><code>public class Base {
      private final Map&lt;String, Command&gt; availableCommands;
      public Base() {
          this.setCommands();  
      }
      private void setCommands() {
          this.availableCommands = Helper.loadCommands();
      }
  }
  </code></pre>
  
  <p>But now I cannot maintain the final modifier and get a compiler error (Final variable cannot be set)</p>
  
  <p>Another way to do this would be:</p>
  
  <pre><code>public class Base {
      private final Map&lt;String, Command&gt; availableCommands = new HashMap&lt;String, Command&gt;();
      public Base() {
          this.setCommands();
      }
      private void setCommands() {
          Helper.loadCommands(availableCommands);
      }
  }
  </code></pre>
  
  <p>But in this case the method in the Helper class would change to:</p>
  
  <pre><code>public static void loadCommands(Map&lt;String, Command&gt; commands) {
      commands.put("A", new CommandA());
      commands.put("B", new CommandB());
      commands.put("C", new CommandC());
  }
  </code></pre>
  
  <p><strong>So the difference is where do I create a new map with <code>new HashMap&lt;String, Command&gt;();</code>?</strong> My main question is if there is a recommended way to do this, given that part of the functionality comes from this Helper's static method, as a way to load the actual map with entries?</p>
  
  <p><strong>Do I create the new map in my Base class or the Helper class?</strong> In both cases Helper will do the actual loading and Base's reference to the map holding the concrete commands will be private and final.</p>
  
  <p>Are there perhaps other more elegant ways to do this besides the options I am considering?</p>