title: Changing a variable value inside a void method to be used outside in Java?
body: <p>I'm new to Java. I searched for this, but didn't find a clear answer.</p>
  
  <p>Is there a way to change the value of a predefined variable inside of a void method and use the new value by another void method?</p>
  
  <p><strong><em>What I need:</em></strong> In Eclipse WindowBuilder, clicking a button should change the value of a variable defined outside of this button. <strong><em>So I can use the new value when clicking another button.</em></strong> However, what happens is that when I click the other button, the initially defined value is used, and not the changed one.</p>
  
  <p><strong>Update: Sample Code:</strong></p>
  
  <pre><code>private void initialize() {
          frame = new JFrame();
          frame.setBounds(100, 100, 450, 300);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().setLayout(null);
          String x = "0";
  
          JButton btn1 = new JButton("Button 1");
          btn1.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  String x = "1";
                  textField1.setText(x);              
              }
          });
          btn1.setBounds(102, 134, 89, 23);
          frame.getContentPane().add(btn1);
  
          JButton btn2 = new JButton("Button 2");
          btn2.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  textField2.setText(x);
              }
          });
          btn2.setBounds(232, 134, 89, 23);
          frame.getContentPane().add(btn2);
  
          textField1 = new JTextField();
          textField1.setBounds(159, 85, 86, 20);
          frame.getContentPane().add(textField1);
          textField1.setColumns(10);
  
          textField2 = new JTextField();
          textField2.setColumns(10);
          textField2.setBounds(159, 179, 86, 20);
          frame.getContentPane().add(textField2);
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/SLXNh.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SLXNh.jpg" alt="enter image description here"></a></p>
  
  <p>So here <code>x</code> is initialized as <code>"0"</code>. Clicking <em>button 1</em>, changes <code>x</code> to <code>"1"</code>. Then, clicking <em>button 2</em>, gives the initialized value which is <code>"0"</code> and not <code>"1"</code>.</p>