title: Java Access Main Class Variables
body: <p>I'm making a small game in which the Main class holds all the objects and variables and calls the methods within the classes themselves that do most of the work. Pretty standard. Unfortunately, that means that many of the variables I need are in the Main class where I can't access them.</p>
  
  <p>For instance, as a test I wanted a ball to bounce around the screen, simple enough, but I need the dimensions of the screen, which I can get easily using the <code>getSize()</code> method in the main class. But when I create the <code>Ball</code> class which will bounce around, I can't access the <code>getSize()</code> method because it is in the <code>Main</code> class. Is there anyway to call it?</p>
  
  <p>I know I can pass the variables to the <code>Ball</code> class in the constructor or for each method I need, but I want to see if there is some way I can take whichever variables I need when I need them, rather than passing it all the information whenever I make a new object.</p>
  
  <p><strong>Main.class</strong></p>
  
  <pre><code>public void Main extends JApplet {
      public int width = getSize().width;
      public int height = getSize().height;
  
      public void init(){
          Ball ball = new Ball();
      }
  }
  </code></pre>
  
  <p><strong>Ball.class</strong></p>
  
  <pre><code>public void Ball {
      int screenWidth;
      int screenHeight;
  
      public Ball(){
          //Something to get variables from main class
      }
  }
  </code></pre>