title: How do I get some variable from another class in Java?
body: <p>I am "playing around" with Java, watching tutorials and trying to get the hang of it. For this question, I'm trying to figure out how I can take a variable from another class and use it in my main one, without making the initial variable public. Here is the code:</p>
  
  <p>I am trying to get <code>int x</code> equal to 5 (as seen in the <code>setNum()</code> method), but when it prints it gives me 0.</p>
  
  <p>Main Class:</p>
  
  <pre><code>package getVarTest;
  
  public class Main { 
      public static void main (String[]args){
          Vars varsObject = new Vars();
          int x = varsObject.getNum();
          System.out.println(x);
      }
  }
  </code></pre>
  
  <p>Variable Class:</p>
  
  <pre><code>package getVarTest;
  
      public class Vars {
          private int num;
              public void setNum(int x){
                  this.num = 5;
              }
              public int getNum(){
                  return num;
              }
      }
  </code></pre>
  
  <p>So, as you can see I am trying to take the <code>private int num</code> and make the <code>int x</code> in the main class equal to it.</p>