title: Access variables from class to class
body: <p>How do i access some variables from another class? For example, i have 2 files:</p>
  
  <p><strong>Main.java</strong></p>
  
  <pre><code>package keyboardgame;
  
  import java.applet.*;
  import java.awt.*;
  import java.awt.event.*;
  
  public class Main extends Applet implements KeyListener
  {
  
      private static final long serialVersionUID = 1L;
      private static boolean keyboardRightPressed = false;
      private static boolean keyboardLeftPressed = false;
      private static boolean keyboardUpPressed = false;
      private static boolean keyboardDownPressed = false;
  
      int ballX = 20; 
      int ballY = 20;
      int ballSpeed = 10;
  
      public void init()
      {
          addKeyListener(this);
      }
  
      public void keyPressed(KeyEvent e) 
      {
          int keyCode = e.getKeyCode();
          if(keyCode == KeyEvent.VK_RIGHT)
          {
              keyboardRightPressed = true;
              ballX = ballX + ballSpeed;
              repaint();
          }
  
          if(keyCode == KeyEvent.VK_LEFT)
          {
              keyboardLeftPressed = true;
              ballX = ballX - ballSpeed;
              repaint();
          }
  
          if(keyCode == KeyEvent.VK_UP)
          {
              keyboardUpPressed = true;
              ballY = ballY - ballSpeed;
              repaint();
          }
  
          if(keyCode == KeyEvent.VK_DOWN)
          {
              keyboardDownPressed = true;
              ballY = ballY + ballSpeed;
              repaint();
          }
      }
  
      public void keyReleased(KeyEvent e) 
      {
          int keyCode = e.getKeyCode();
          if(keyCode == KeyEvent.VK_RIGHT)
          {
              keyboardRightPressed = false;
              repaint();
          }
  
          if(keyCode == KeyEvent.VK_LEFT)
          {
              keyboardLeftPressed = false;
              repaint();
          }
  
          if(keyCode == KeyEvent.VK_UP)
          {
              keyboardUpPressed = false;
              repaint();
          }
  
          if(keyCode == KeyEvent.VK_DOWN)
          {
              keyboardDownPressed = false;
              repaint();
          }
      }
  
      public void keyTyped(KeyEvent e) {
      }
  
  
      public void paint(Graphics g)
      {
          g.fillOval(ballX,ballY,20,20);
          g.drawString("Right :"+keyboardRightPressed,0,10);
          g.drawString("Left :"+keyboardLeftPressed,0,20);
          g.drawString("Up :"+keyboardUpPressed,0,30);
          g.drawString("Down :"+keyboardDownPressed,0,40);        
      }
  
  }
  </code></pre>
  
  <p><strong>KeyThread.java</strong></p>
  
  <pre><code>package keyboardgame;
  
  public class KeyThread implements Runnable
  {
  
      public KeyThread(String s,int speed)
      {
          name = s;
          time = speed;
      }
  
      public void run() 
      {
          try
          {
              if(keyboardRightPressed == true)
              {
                  ballX = ballX + 10; 
                  ballY = ballY + 10;
              }
          }
          catch(Exception e)
          {
  
          }
      }
  }
  </code></pre>
  
  <p>What do i want is that from <strong>KeyThread.java</strong> to be able to modify/increment/access values from <strong>Main.java</strong> especially </p>
  
  <pre><code>int ballX = 20; 
  int ballY = 20;
  </code></pre>
  
  <p>so i can call my thread from <strong>Main.java</strong> like</p>
  
  <pre><code>Thread t5 = new Thread(new KeyThread("Moving Right", ballX = ballX + 1, 20));
  t5.start();
  </code></pre>
  
  <p>How can i do that?
  And why is it so hard to do it?</p>