title: Creating global array of objects (java)
body: <p>So I'm working on a Space Invaders theme project, and I have most of my classes up and running and have started on the animation. Part of the process is the ship's weapons.</p>
  
  <p>I have a class for the weapons, as below (Focus on the constructor):</p>
  
  <pre><code>    /**
   * @(#)Weapon.java
   *
   *
   * @author Tristan Nel - 18179460
   * @version 1.00 2015/3/4
   */
  
  
  public class Weapon {
  
      private String type;
      private int damage;
      private int rof; //rate of fire
      private int orientation;
      private int firingStage; //0 - not firing ; 1 - flash &amp; recoil ; 2 - bullet
      private String[] sprites; //Set of sprite image file names
  
      public Weapon() {
      }
  
      public Weapon(String type, int damage, int rof, int orientation, int firingStage, String[] sprites)
      {
          this.type = type;
          this.damage = damage;
          this.rof = rof;
          this.orientation = orientation;
          this.firingStage = firingStage;
          this.sprites = sprites;
      }
  
      //GET and SET Methods
      public void setType(String type)
      {
          this.type = type;
      }
  
      public void setDamage(int damage)
      {
          this.damage = damage;
      }
  
      public void setROF(int rof)
      {
          this.rof = rof;
      }
  
      public void setOrientation(int orientation)
      {
          this.orientation = orientation;
      }
  
      public void setFiringStage(int firingStage)
      {
          this.firingStage = firingStage;
      }
  
      public void setSprites(String[] sprites)
      {
          this.sprites = sprites;
      }
  
      public String getType()
      {
          return this.type;
      }
  
      public int getDamage()
      {
          return this.damage;
      }
  
      public int getROF()
      {
          return this.rof;
      }
  
      public int getOrientation()
      {
          return this.orientation;
      }
  
      public int getFiringStage()
      {
          return this.firingStage;
      }
  
      public String[] getSprites()
      {
          return this.sprites;
      }
  
  }
  </code></pre>
  
  <p>In another class, which handles all elements on the game screen to be animated, I want to have a global array of hardcoded Weapon types that can be accessed as needed without fuss. I have attempted to do so at the top of the contents of the class:</p>
  
  <pre><code>    /**
   * @(#)GameScreen.java
   *
   *
   * @author Tristan Nel - 18179460
   * @version 1.00 2015/3/4
   */
  
  import java.util.Scanner;
  import java.io.*;
  
  public class GameScreen {
  
      private static final String HIGH_SCORE_FILE = "highScore.txt";
  
      //Available Weapons
      //UPDATED SINCE ORIGINAL POST
      public static final Weapon[] WEAPONS = new Weapon[4];
      WEAPONS[0] = new Weapon("Machinegun",       10, 20, 0, 0,   {Graphics.MG_L_NORM, Graphics.MG_R_NORM});
      WEAPONS[1] = new Weapon("Plasma MG",        20, 20, 0, 0,   {Graphics.PMG_L_NORM, Graphics.PMG_R_NORM});
      WEAPONS[2] = new Weapon("Photon Cannon",        40, 5, 0, 0,    {Graphics.PC_L_NORM, Graphics.PC_R_NORM});
      WEAPONS[3] = new Weapon("Alien Destabilizer",   60, 10, 0, 0,   {Graphics.AD_L_NORM, Graphics.AD_R_NORM});
  
      private Ship defender;
      private Weapon equipped;
      //private Invader[] aliens;
      //private Bullet[] bullets;
      private int score;
      private int highscore;
      private int lives;
  
      public GameScreen() {
      }
  
      public GameScreen(Ship defender, int score, int lives)
      {
          this.defender = defender;
          this.score = score;
          this.lives = lives;
      }
  
      public void loadHighscore()
      {
          try
          {
              Scanner sc = new Scanner(new File(HIGH_SCORE_FILE));
              this.highscore = Integer.parseInt(sc.next());
              sc.close();
          }
          catch(FileNotFoundException fnf)
          {
              System.out.println(fnf);
              this.highscore = 0;
          }
  
      }
  
      public void saveHighScore(int highscore)
      {
          try
          {
              FileWriter write = new FileWriter(HIGH_SCORE_FILE);
              PrintWriter pw = new PrintWriter(write);
              pw.print(this.highscore);
  
              pw.close();
          }
          catch(IOException e)
          {
              System.out.println(e);
          }
      }
  
      //GET and SET methods
      public void setDefender(Ship defender)
      {
          this.defender = defender;
      }
  
      public void setScore(int score)
      {
          this.score = score;
      }
  
      public void setLives(int lives)
      {
          this.lives = lives;
      }
  
      public Ship getDefender()
      {
          return this.defender;
      }
  
      public int getScore()
      {
          return this.score;
      }
  
      public int getLives()
      {
          return this.lives;
      }
  
  }
  </code></pre>
  
  <p>This gives me the following error messages on each line that I try to add another element to the array:</p>
  
  <p>UPDATED
  <a href="https://drive.google.com/file/d/0B7ye7Ul2JDG2NDFDRTJNM1FCd0U/view?usp=sharing" rel="nofollow">https://drive.google.com/file/d/0B7ye7Ul2JDG2NDFDRTJNM1FCd0U/view?usp=sharing</a></p>
  
  <p>It is highly frustrating..
  I read somewhere that you have to create an object within a method? (Eg. main() )
  But I tried that in my driver class and it made no difference...</p>
  
  <p>Will appreciate any help/advice (:</p>