title: Why use public static textfields in swing
body: <p>I had a problem, im currently in learning process of java swing/awt </p>
  
  <p>I was creating a small program, with 2 jtextfields and 1 jbutton</p>
  
  <p>I used 2 classes, 1 was for GUI and other was for LOGIC/IMPLEMENTATIONS </p>
  
  <p>I declared a button in class1 and applied an actionlistener to it, in class2(extends class1) i created a method that was doing this comparison </p>
  
  <p>MaisamCustom is my class1 and LogicClass is class2</p>
  
  <p><img src="https://i.stack.imgur.com/kkbV4.png" alt="enter image description here"></p>
  
  <p>this screen shot is from MaisamCustom(Class1), the button in which this method is being called </p>
  
  <p><img src="https://i.stack.imgur.com/ZPAh3.png" alt="enter image description here"></p>
  
  <p>Now the real problem, when ever I entered 2 different/same values and press the button, it was displaying the message in IF statement, else was not working</p>
  
  <p>BOTH FIELDS MATCH !!!</p>
  
  <p>so i googled it, after hours of search i found an answer on stackoverflow</p>
  
  <p>this guy called @Azuu answered a similiar question with ease </p>
  
  <p>--> <a href="https://stackoverflow.com/questions/12347616/get-value-from-jpanel-textfield-in-another-class">Get value from JPanel textfield in another class</a></p>
  
  <p><img src="https://i.stack.imgur.com/urK1u.png" alt="enter image description here"></p>
  
  <p>so i did the same thing with my JTEXTFIELDS objects declaration and IT WORKED !! :')
  <img src="https://i.stack.imgur.com/cn0fz.png" alt="enter image description here"></p>
  
  <p>I'm happy , and really want to thank this guy ( @Azuu ).</p>
  
  <p>now the problem</p>
  
  <p>I know what static does and what is public</p>
  
  <p>but how did my program started to work perfect by just making jtextfields <strong>public static</strong></p>
  
  <p>can anyone explain this to me please :)</p>
  
  <p>and yes, here is the code 
  ( its really messed up because I'm experimenting different aspects of GUI on it )</p>
  
  <h2>Class 1</h2>
  
  <pre><code>package gui.examples;
  
  
  import java.awt.event.*;
  import javax.swing.*;
  
  public class MaisamCustom {
  
      JFrame frame = new JFrame("My Desktop App");
      JPanel panel = new JPanel();
      public static JTextField txt1 = new JTextField(8),
              txt2 = new JTextField(8);
      JButton enter_btn = new JButton("Enter");
      public void launchFrame() {
  
          JLabel label1 = new JLabel("   "),
                  label3 = new JLabel("   "),
                  label4 = new JLabel("   "),
                  label5 = new JLabel("   "),
                  label2 = new JLabel("                  My Comparision Program");
          panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
          panel.add(label3);
          panel.add(label2);
          panel.add(label4);
          panel.add(txt1);
          panel.add(label1);
          panel.add(txt2);
          panel.add(label5);
          enter_btn.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  LogicClass obj = new LogicClass();
                  obj.enterButton();
  
              }
          });
          panel.add(enter_btn);
          frame.setResizable(false);
          frame.getContentPane().add(panel);
          frame.pack();
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      }
  
  
      public static void main(String[] args) {
          MaisamCustom obj = new MaisamCustom();
          try {
              obj.launchFrame();
          } catch (Exception ex) {
              System.out.println("Some issue while launching the application...");
          }
      }
  
  
  }
  </code></pre>
  
  <h2>Class2</h2>
  
  <pre><code>package gui.examples;
  
  import javax.swing.JOptionPane;
  
  public class LogicClass extends MaisamCustom {
  
      public void info(String message, String title) {
          JOptionPane.showMessageDialog(null, message, "PromptBox: " + title, JOptionPane.INFORMATION_MESSAGE);
      }
  
      public void enterButton() {
          LogicClass obj = new LogicClass();
          if (txt1.getText().equals(txt2.getText())) {
              obj.info("Both Fields Match !!!", "Note !!");
          } else {
              obj.info("Both Fields Do Not Match !!!", "Note !!");
          }
      }
  
  }
  </code></pre>
  
  <blockquote>
    <p>im really sorry , my question is so simple but i have to explain it
    into detail so that is what making it so long, i actually love to
    explain my problems step by step and with details</p>
  </blockquote>
  
  <p>i posted a question in same style months ago ( sometimes users get rude and they scold you really hard so this all explanations are just to prevent that )</p>
  
  <p><img src="https://i.stack.imgur.com/DqGKW.png" alt="enter image description here"></p>