title: best practice to avoid static fields
body: <p>I've already read some questions about the disadvantages of static variables (for example <a href="https://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil">why are static variables considered evil</a>, but i find some difficulties in choosing the best solution to avoid to use them.</p>
  
  <p>In my simple chess application, for example, i have an abstract class, Piece, with many subclasses.
  Every Piece must have a BufferedImage variable, <em>image</em>, but <strong>i want to load every image just one time</strong> for each Piece.</p>
  
  <p>This would be very easy by using static variables, something like that :</p>
  
  <pre><code>abstract class Piece
  {
      // ... 
      public abstract BufferedImage getImage();
  }
  class Bishop extends Piece
  {
      private static final BufferedImage image = null;
  
      static{
          try{
              image = ImageIO.read(ClassLoader.getSystemResource("chess/img/pieces/Bishop.png"));
          }
          catch(IOException ex){
              // ...
          }
      }
  
      public BufferedImage getImage(){
          return image;
      }
  }
  </code></pre>
  
  <p>Of course this code doesn't follow OOP, and inheritance is not used for the <em>image</em> variable.</p>
  
  <p>A solution would be to load all the images in an external class, and let all the subclasses instances to keep the reference to the same object:</p>
  
  <pre><code>abstract class Piece
  {
      final BufferedImage image;
      public Piece(/* ... */ BufferedImage image){
          this.image = image;
      }
      public BufferedImage getImage(){
          return image;
      }
  }
  class Bishop extends Piece
  {
      public Bishop(/* ... */ BufferedImage image){
          super(/* ... */ image);
          // ...
      }
  }
  </code></pre>
  
  <p>But what if i need to create the subclasses instances from <em>n</em> different classes ?
  The images would be loaded at least <em>n</em> times, assuming i create just one instance of those <em>n</em> classes.</p>
  
  <p>I've considered to keep the images reference in a Singleton pattern designed class, but i also want to keep the <em>image</em> variable in the Piece class to simply use the getImage() method for every Piece.</p>
  
  <p>Is it a good design to use the second solution combined with the Singleton pattern, so i would pass to every Piece's subclass constructor the reference to the same BufferedImage obtained with a method like SingletonClass.getXxxImage()?</p>
  
  <p>Or is there any better approach? Of course it would be not so evil to load a BufferedImage more than one time, but i would like to have a general solution to avoid useless repetitions of code.</p>
  
  <p>Thanks for your help !</p>