title: Protected Variables aren't allowed by default in Checkstyle, but what about in an enum?
body: <p>I'm using Java in eclipse, and I read a <a href="https://stackoverflow.com/questions/8465450/why-are-protected-variables-not-allowed-by-default-in-checkstyle">thread</a></p>
  
  <p>I really liked <a href="https://stackoverflow.com/users/834362/home">home's</a> <a href="https://stackoverflow.com/questions/8465450/why-are-protected-variables-not-allowed-by-default-in-checkstyle#answer-8465473">answer</a>, because I never really thought about it before. The whole idea that subclasses should really not have access to members that is. However, I have an interesting edge case if someone wants to give it a go.</p>
  
  <p>Suppose I have an interface <code>Buildable</code> and an Enum <code>CatanPiece</code>:</p>
  
  <pre><code>public interface Buildable
  {
      HashMap&lt;Resource, Integer&gt; getCost();
      Buildable build( final PlayerHand payment );
  }
  
  public enum CatanPiece implements Buildable
  {
      ROAD
      {
          @Override
          public HashMap&lt;Resource, Integer&gt; getCost()
          {
              if ( cost.isEmpty() )
              {
                  cost.put( BRICK, 1 );
                  cost.put( LUMBER, 1 );
              }
              return cost;
          }
      },
  
      SETTLEMENT
      {
          @Override
          public HashMap&lt;Resource, Integer&gt; getCost()
          {
              if ( cost.isEmpty() )
              {
                  cost.put( BRICK, 1 );
                  cost.put( LUMBER, 1 );
                  cost.put( SHEEP, 1 );
                  cost.put( WHEAT, 1 );
              }
              return cost;
          }
      },
  
      CITY
      {
          @Override
          public HashMap&lt;Resource, Integer&gt; getCost()
          {
              if ( cost.isEmpty() )
              {
                  cost.put( WHEAT, 2 );
                  cost.put( ORE, 3 );
              }
              return cost;
          }
      };
  
      protected final HashMap&lt;Resource, Integer&gt; cost;
  
      private CatanPiece()
      {
          cost = getCost();
      }
  
      @Override
      public abstract HashMap&lt;Resource, Integer&gt; getCost();
  
      @Override
      public Buildable build( final PlayerHand payment )
      {
          return ( payment.remove( cost ) ? null : this );
      }
  }
  </code></pre>
  
  <p>So Checkstyle is giving me crap about the protected HashMap I'm using, but for those looking at this question that understand it, you can see I don't have the problem of someone misusing that variable. AND I would in fact make it private and use a protected get method, however the return on that method is different per instance.</p>
  
  <p>Potential answers: Ignore the checkstyle warning, or perhaps include an abstract <em>init()</em> method to initialize the HashMap then simply implement the <em>getCost()</em> method generically for all memebers of the enum.</p>
  
  <p>What do you think?</p>