title: How do we change the value of a public variable inside a method?
body: <p><strong>Here is an example:</strong>
  How to change the value of x from &quot;<em>a</em>&quot; to &quot;<em>b</em>&quot; inside a method to get the result &quot;<em>b</em>&quot; outside of it?</p>
  <pre><code>public class Main {
  
      String x = &quot;a&quot;;
      public static void main(String[] args) {
          new Main();
      }
  
      public Main(){
          System.out.println(x); //&quot;a&quot;
          String x = &quot;b&quot;;
          System.out.println(x); //&quot;b&quot;
          new Thread(new Runnable() {
              public void run() {
                  String x = &quot;c&quot;; 
                  System.out.println(x); //&quot;c&quot;
              }
          }).start();
          System.out.println(x); //&quot;b&quot;
      }
  
      public void method(){
          System.out.println(x); //&quot;a&quot;
      }
  }
  </code></pre>