title: Behavior of static methods/variables change after adding instance variable in Java
body: <p>This isn't so much of a problem as it is a question. I have several classes that inherit the following abstract class:</p>
  
  <pre><code>public abstract class PixelEditorWindow {
      protected static int windowHeight, windowWidth;
  
      public PixelEditorWindow() {
          setWindowSize();
      }
  
      protected abstract void setWindowSize();
  
      public static int getWindowWidth() {
          return windowWidth;
      }
  
      public static int getWindowHeight() {
          return windowHeight;
      }
  }
  </code></pre>
  
  <p>I have each class inherit the abstract method setWindowSize, where they set what their window size will be, and these two variables are set, as follows:</p>
  
  <pre><code>protected void setWindowSize() {
      windowWidth = 400;
      windowHeight = 400;
  }
  </code></pre>
  
  <p>That works perfectly. But I also need all of them to keep track of the JFrame they're contained in now, so I modified this class in the following way:</p>
  
  <pre><code>public abstract class PixelEditorWindow {
      protected int windowHeight, windowWidth;
      JFrame frame;
  
      public PixelEditorWindow(JFrame frame) {
          this.frame = frame;
          setWindowSize();
      }
  
      public JFrame getFrame() {
          return frame;
      }
  
      protected abstract void setWindowSize();
  
      public int getWindowWidth() {
          return windowWidth;
      }
  
      public int getWindowHeight() {
          return windowHeight;
      }
  }
  </code></pre>
  
  <p>Now, all inherited classes have the same window size, which is whatever type of window was instantiated last. <strike>I can fix it by having another class that inherits this one that all the sub-classes inherit, but that's messier than I'd like. </strike>EDIT: just realized I can't do that either, but there are lots of other messy ways to work around this. Why is it that adding a non-static instance variable mucks everything up? Or a better question I suppose - why is it that not having a non-static instance variable allows this to work?</p>