title: Why cant I pass a variable from one .java file to another?
body: <p>There is something odd going on in my code. I'm making a Connect Four game, but making it to where you can have up to eight teams with four players each. There are three separate GUI class files, each with their own layout, etc. But I cannot pass a variable from the SettingsGUI file to the TeamSettingsGUI file, despite my best efforts. What is weird, is how I can initialize the variable, or have the getXXX method return a certain number and it works fine. But I am unable to pass the value of the variable, for some reason.</p>
  
  <p>The two variables I am dealing with right now are numTeams and playersPerTeam. IF I could understand how to get one of them to work, I think I could get the rest of them to work as well. I'm sure it's something simple, but I'm stumped.
  Here's the int ital GUI, SettingsGUI:</p>
  
  <pre><code>package connectfouradvanced;
  
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.*;
  
  public class SettingsGUI extends JPanel implements ActionListener {
  
  JLabel saveLabel = new JLabel("Saved. Please exit this GUI.");
  JButton save = new JButton("Save");
  
      JLabel numTeamsLabel = new JLabel("Chose the number of teams.");
      Choice numTeamsChoice = new Choice();
  
      JLabel boardDimensionsLabel = new JLabel("Chose the board dimensions.");
      Choice boardDimensionsChoice = new Choice();
  
      JLabel numPlayersPerTeamLabel = new JLabel("Chose the number of players per team.");
      Choice numPlayersPerTeamChoice = new Choice();
  
      JLabel powerupsLabel = new JLabel("Powerups On/Off?");
      JCheckBox powerups = new JCheckBox();
  
      JLabel standardLabel = new JLabel("Standard Setup?");
      JCheckBox standardSettings = new JCheckBox();
  
      int numTeams, boardRows, boardColumns, playersPerTeam; 
      //if you were to put numTeams = 4, then 4 would be passed to TeamSettingsGUI
      boolean powerupsSelected = false, standardSelected;
  
  public SettingsGUI() {
      add(saveLabel);
      saveLabel.setVisible(false);
  
      add(save);
      save.addActionListener(this);
  
      add(numTeamsLabel);
      add(numTeamsChoice);
      numTeamsChoice.add("2");
      numTeamsChoice.add("3");
      numTeamsChoice.add("4");
      numTeamsChoice.add("5");
      numTeamsChoice.add("6");
      numTeamsChoice.add("7");
      numTeamsChoice.add("8");
  
      add(boardDimensionsLabel);
      add(boardDimensionsChoice);
      boardDimensionsChoice.add("7x6");
      boardDimensionsChoice.add("14x12");
      boardDimensionsChoice.add("21x18");
      boardDimensionsChoice.add("28x24");
      boardDimensionsChoice.add("35x30");
      boardDimensionsChoice.add("42x36");
      boardDimensionsChoice.add("49x42");
      boardDimensionsChoice.add("56x48");
      boardDimensionsChoice.add("63x54");
      boardDimensionsChoice.add("70x60");
  
      add(numPlayersPerTeamLabel);
      add(numPlayersPerTeamChoice);
      numPlayersPerTeamChoice.add("1");
      numPlayersPerTeamChoice.add("2");
      numPlayersPerTeamChoice.add("3");
      numPlayersPerTeamChoice.add("4");
  
      add(powerupsLabel);
      add(powerups);
      powerups.setSelected(true);
  
      add(standardLabel);
      add(standardSettings);
  }
  
  @Override
  protected void paintComponent(Graphics g) { 
      super.paintComponent(g);
  
      saveLabel.setLocation(150, 100);
      save.setLocation(315, 90);
  
      numTeamsLabel.setLocation(0, 0);
      numTeamsChoice.setLocation(170, 0);
      numTeamsChoice.setSize(40, 15);
  
      boardDimensionsLabel.setLocation(0, 25);
      boardDimensionsChoice.setLocation(170, 25);
      boardDimensionsChoice.setSize(80, 15);
  
      numPlayersPerTeamLabel.setLocation(0, 50);
      numPlayersPerTeamChoice.setLocation(225, 50);
      numPlayersPerTeamChoice.setSize(40, 15);
  
      powerupsLabel.setLocation(0, 75);
      powerups.setLocation(110, 75);
  
      standardLabel.setLocation(0, 100);
      standardSettings.setLocation(110, 100);
  }
  
  public void saveSettings() {
      switch(numTeamsChoice.getSelectedIndex()) {
          case 0: numTeams = 2; break;
          case 1: numTeams = 3; break;
          case 2: numTeams = 4; break;
          case 3: numTeams = 5; break;
          case 4: numTeams = 6; break;
          case 5: numTeams = 7; break;
          case 6: numTeams = 8; break;
      }
  
      switch(boardDimensionsChoice.getSelectedIndex()) {
          case 0: boardColumns = 7; boardRows = 6; break;
          case 1: boardColumns = 14; boardRows = 12; break;
          case 2: boardColumns = 21; boardRows = 18; break;
          case 3: boardColumns = 28; boardRows = 24; break;
          case 4: boardColumns = 35; boardRows = 30; break;
          case 5: boardColumns = 42; boardRows = 36; break;
          case 6: boardColumns = 49; boardRows = 42; break;
          case 7: boardColumns = 56; boardRows = 48; break;
          case 8: boardColumns = 63; boardRows = 54; break;
          case 9: boardColumns = 70; boardRows = 60; break;
      }
  
      switch(numPlayersPerTeamChoice.getSelectedIndex()) {
          case 0: playersPerTeam = 1; break;
          case 1: playersPerTeam = 2; break;
          case 2: playersPerTeam = 3; break;
          case 3: playersPerTeam = 4; break;
      }
  
      if(powerups.isSelected() == true) {
          powerupsSelected = true;
      }
  
      if(standardSettings.isSelected() == true) {
          standardSelected = true;
          powerupsSelected = false;
          numTeams = 2;
          boardColumns = 7;
          boardRows = 6;
          playersPerTeam = 1;
      }
  
      saveLabel.setVisible(true);
  
      System.out.println("Using standard settings? " + standardSelected);
      System.out.println("Powerups enabled? " + powerupsSelected);
      System.out.println(numTeams + " " + boardColumns + " " + boardRows + " " + playersPerTeam);
  }
  
  @Override
  public void actionPerformed(ActionEvent e) {
      if(e.getSource().equals(save)) {
          saveSettings();
      }
  }
  
  public int getBoardColumns() {
      return boardColumns;
  }
  
  public int getBoardRows() {
      return boardRows;
  }
  
  public int getNumTeams() {
      return numTeams;
      //again, put return 4; here and it will return 4 to TeamSettingsGUI
  }
  
  public int getPlayersPerTeam(){
      return playersPerTeam;
  }
  
  public boolean getPowerupsEnabled() {
      return powerupsSelected;
  }
  
  public boolean getStandardEnabled() {
      return standardSelected;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Now remember, where I initialize numTeams and playersPerTeam, it can pass through to TeamSettingsGUI perfectly. I have a feeling that in saveSettings() it's not saving the variable, it's only temporary. If it is, then I don't know how to fix it. I've tried stuff like this.numPlayers in the switch case and everything.</p>
  
  <p>Here is the TeamSettingsGUI:</p>
  
  <pre><code>package connectfouradvanced;
  
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.*;
  
  public class TeamSettingsGUI extends JPanel implements ActionListener {
  
      SettingsGUI settings = new SettingsGUI();
      int numTeams = settings.getNumTeams();
      int playersPerTeam = settings.getPlayersPerTeam();
  
      JButton saveTeam = new JButton();
  
      Color[] teamColors = new Color[numTeams];
      String[][] playerNames = new String[numTeams][playersPerTeam];
      char[][] playerSymbols = new char[numTeams][playersPerTeam];
  
      Choice[] colors = new Choice[numTeams];
      String[] colorList = {"Red", "Yellow", "Orange", "Green", "Black", "Blue", "Cyan",     "Magenta"};
  
      JCheckBox[] ready = new JCheckBox[numTeams];
      JLabel[] teamNumbers = new JLabel[numTeams];
      JTextField[][] playerNamesInput = new JTextField[numTeams][playersPerTeam];
      JTextField[][] playerSymbolsInput = new JTextField[numTeams][playersPerTeam];
  
  public TeamSettingsGUI() {
      add(saveTeam);
      saveTeam.addActionListener(this);
  
      for(int i = 0; i &lt; teamNumbers.length; i++) {
          teamNumbers[i] = new JLabel();
          teamNumbers[i].setText("Team " + (i + 1));
          add(teamNumbers[i]);
      }
  
      for(int i = 0; i &lt; colors.length; i++) {
          colors[i] = new Choice();
          colors[i].add("Red");
          colors[i].add("Yellow");
          colors[i].add("Orange");
          colors[i].add("Green");
          colors[i].add("Black");
          colors[i].add("Blue");
          colors[i].add("Cyan");
          colors[i].add("Magenta");
          colors[i].select(colorList[i]);
          add(colors[i]);
      }
  
      for(int i = 0; i &lt; playerNamesInput.length; i++) {
          for(int k = 0; k &lt; playerNamesInput[i].length; k++) {
              playerNamesInput[i][k] = new JTextField();
              playerNamesInput[i][k].setText("Team " + (i+1) + " player " + (k+1) + " name: ");
              add(playerNamesInput[i][k]);
          }
      }
  
      for(int i = 0; i &lt; playerSymbolsInput.length; i++) {
          for(int k = 0; k &lt; playerSymbolsInput[i].length; k++) {
              playerSymbolsInput[i][k] = new JTextField();
              playerSymbolsInput[i][k].setText("Team " + (i+1) + " player " + (k+1) + " symbol: ");
              add(playerSymbolsInput[i][k]);
          }
      }
  
      for(int i = 0; i &lt; ready.length; i++) {
          ready[i] = new JCheckBox();
          ready[i].setText("Ready?");
          add(ready[i]);
      }
  }
  
  @Override
  public void actionPerformed(ActionEvent e) {
      if(e.getSource().equals(saveTeam)) {
  
      }
  }
  
  @Override
  protected void paintComponent(Graphics g) { 
      super.paintComponent(g);
      System.out.println(numTeams + " " + playersPerTeam);
  }
  </code></pre>
  
  <p>Note, TeamSettingsGUI is NOT complete. I can't continue on with aligning things and whatnot until I fix this.
  Sorry if my code is dirty or anything, but I just cant figure out what's going on.</p>
  
  <p>Forgot to include the main executable class, there is another missing BoardGUI class that is empty for now.</p>
  
  <pre><code>    package connectfouradvanced;
      import javax.swing.*;
      public class ConnectFourAdvanced {
  
      public static void main(String[] args) {
      SettingsGUI s = new SettingsGUI();
      BoardGUI b = new BoardGUI();
      TeamSettingsGUI t = new TeamSettingsGUI();
  
      JFrame fs = new JFrame();
      JFrame fb = new JFrame();
      JFrame ft = new JFrame();
      fs.add(s);
      fb.add(b);
      ft.add(t);
  
      fs.setVisible(true);
      fs.setResizable(false);
      fs.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
      fs.setLocationRelativeTo(null);
      fs.setSize(400, 150);
      fs.setTitle("Settings");
  
      while(fs.isVisible() == true) {
          ft.setVisible(false);
      }
  
      ft.setVisible(true);
      ft.setResizable(false);
      ft.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      ft.setLocationRelativeTo(null);
      ft.setSize(1200, 500);
      ft.setTitle("Team Settings");
  
      while(ft.isVisible() == true || fs.isVisible() == true) {
          fb.setVisible(false);
      }
  
      fb.setVisible(true);
      fb.setResizable(false);
      fb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      fb.setLocation(100, 100);
      fb.setSize(1000, 1000);
      fb.setTitle("Connect Four");
  }   
  </code></pre>
  
  <p>}</p>
  
  <p>I would like to add that I am only 16, and while I'm not that bad at java, GUIs and objects are still pretty new to me. So if my code is clunky or cluttered, or I'm doing some pretty dumb things that seem simple to others, it's because I'm new to this. This is my first big GUI project that I started on my own.</p>