title: Java : variable reuse
body: <p>I'm writing a simple Java program.</p>
  <p>First, the program asks user to input some info like name and id of the student and uses radio button for asking whether the student is present or absent. After finishing the inputs, then program validate whether predefined student names and inputs are match or not. And check id number again and spit out if input is over 4. Lastly check radio button is true or false. If one of the above two rules get error then program will quit without executing next method.</p>
  <p>I have three .java files. One for UI. One for validation and one for storing data.</p>
  <p>UI.java</p>
  <pre><code>import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JRadioButton;
  import javax.swing.JTextField;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  public class UI extends JFrame {
      JTextField name = new JTextField(&quot;Name&quot;, 10);
      JTextField id = new JTextField(&quot;Id&quot;, 10);
      JRadioButton attendance = new JRadioButton(&quot;Present&quot;);
      JButton JB = new JButton(&quot;Check&quot;);
  
      public UI() {
          super(&quot;Test&quot;);
          JPanel JP = new JPanel();
          JP.add(name);
          JP.add(id);
          JP.add(attendance);
          JP.add(JB);
          add(JP);
          pack();
          setLocationRelativeTo(null);
          setVisible(true);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
  
      public static void buttonAction(){
          UI UIbutton = new UI();
          UIbutton.JB.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  if (e.getSource() == UIbutton.JB) {
                      String nameInput = UIbutton.name.getText();
                      int idInt = Integer.parseInt(UIbutton.id.getText());
                      boolean attInput = UIbutton.attendance.isSelected();
                      Validate.nameChk(nameInput);
                      Validate.idChk(idInt);
                      Validate.attChk(attInput);
                      Student studentObj = new Student(UIbutton.name.getText(), idInt, UIbutton.attendance.isSelected());
                      System.out.println(studentObj.name + &quot;'s ID number is : &quot; + studentObj.id + &quot;.&quot;);
                      System.out.println(studentObj.name + &quot; is present: &quot; + studentObj.attendance);
                      System.exit(0);
                  }}});
      }
  
      public static void main(String[] args) {
          buttonAction();
      }
  }
  </code></pre>
  <p>Validate.java</p>
  <pre><code>public class Validate {
      public static void nameChk (String nameInput) {
          String n1 = &quot;Matthew&quot;;
          String n2 = &quot;John&quot;;
          String n3 = &quot;Mark&quot;;
          String n4 = &quot;Luke&quot;;
          if ((nameInput.equalsIgnoreCase(n1))||
                  (nameInput.equalsIgnoreCase(n2))||
                  (nameInput.equalsIgnoreCase(n3))||
                  (nameInput.equalsIgnoreCase(n4))){
              System.out.println(&quot;Your data is okay.&quot;);
          }
          else {
              System.out.println(&quot;Error, wrong student name.&quot;);
              System.exit(0);
          }
      }
      public static void idChk (int idInt) {
          if (idInt &gt; 4) {
              System.out.println(&quot;Your id is not correct.&quot;);
              System.exit(0);
          }
          else {
              System.out.println(&quot;Your id is correct.&quot;);
          }
      }
      public static void attChk (boolean attInput) {
          if (attInput) {
              System.out.println(&quot;The student is present.&quot;);
          } else {
              System.out.println(&quot;The student is absent.&quot;);
          }
      }
  }
  </code></pre>
  <p>Student.java</p>
  <pre><code>public class Student {
      String name;
      int id;
      boolean attendance;
  
      Student(String name, int id, boolean attendance) {
          this.name = name;
          this.id = id;
          this.attendance = attendance;
      }
  }
  </code></pre>
  <p>What I want to know is how can I reuse output of that actionlister method somewhere else. Let say I would create foo.java class and use that studentObj variable to give grades like</p>
  <pre><code>System.out.println(studentObj.name+&quot;has B+.&quot;);
  </code></pre>
  <p>Sort of.
  How can I do that? How to turn that variable into global?</p>