title: How can I get my score in an other class? JAVA
body: <p>I'm making a game with multiple asteroids going down. The goal is to dodge as many asteroids for as long as you can. I made it so that as soon as an asteroid reaches the bottom of the screen it is send back up, now i want to count every time it gets to the bottom and add it to the score. 
  My problem is that all the asteroids are the same class so if I use:</p>
  
  <p><code>if(y&gt;700){
      y=-50;                        //
      x= (int) (Math.random()*670); // To send the asteroid back up
      setLocation(x,y);             //
      score++;                   // To add up the score
      System.out.println(score); // To print the score</code></p>
  
  <p>Every asteroid adds op his own amount of times it has reached the bottom, but I want to know how many asteroids have reached the bottom in total. So I figured I have to get the score out of the asteroid class and add it up in an other class but I don't know how.</p>
  
  <p>This is the code of the asteroid class:</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.JPanel;
  import javax.swing.Timer;
  
  public class Astroid extends JPanel implements ActionListener
  {
   public int yVelocity = 1;
   public int x = (int) (Math.random()*650), y = (int) (Math.random()*-1000);
   public Timer timer;
   private int score;
  
   public Astroid(int x,int y)
   {
      this.setLocation(x, y);
      this.setSize(25, 25);
      this.setBackground(Color.WHITE);
      this.setVisible(true);
   }
   {
      this.timer = null;
      this.timer = new Timer(10,new ActionListener(){
          @Override
          public void actionPerformed(ActionEvent arg0)
          {
              setLocation(x,(y+=yVelocity));
              timer.setDelay(10);
              repaint();
  
              if(y&gt;700){
                  y=-50;
                  x= (int) (Math.random()*670);
                  setLocation(x,y);
  
                  score++;
                  System.out.println(score);
              }
          }
      });
      timer.start();
   }
  }
  </code></pre>
  
  <p>This is the code of the class that creates the asteroids:</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.util.ArrayList;
  import java.util.Timer;
  import java.util.TimerTask;
  
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  public class SterTest extends JFrame implements ActionListener
  {
  
  public int a;
  
  public SterTest() 
  {
      this.setSize(700, 700);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setLocationRelativeTo(null);
  
      final JPanel p = new JPanel();
      p.setBackground(Color.BLACK);
      p.setLayout(null);
  
      for(a=0;a&lt;3;a++) {
          Astroid astroid = new Astroid(1,1);
          p.add(astroid);
      } //Creates 3 asteroids to start with
  
      Timer timer = new Timer();
  
      timer.scheduleAtFixedRate(new TimerTask() {
  
          @Override
          public void run() {
              Astroid astroid2 = new Astroid(1,1);
              p.add(astroid2);    
              a++;
              System.out.println("Het aantal asteroids in game is:"+a);
          }               
      }, 5000, 5000); // creates a new asteroid every 5 seconds or so
  
      this.add(p);
  
      this.setVisible(true);
  }
  </code></pre>