title: How to access variables in another package in java?
body: <p>I am trying to access variables in another package in java, I tried some code:</p>
  
  <p>The first package contains two classes, this is the first one</p>
  
  <pre><code>package encapsuation;
  import s2.foo;
  
  public class Encapsulation {
  
      public static void main(String[] args) {
          s1 a = new s1();
          a.age = 21;
          a.name = "ahmed";
  
          a.print();
          foo.print();
      }
  
  }
  </code></pre>
  
  <p>The second one, this is what I need to access in the class of the second package.</p>
  
  <pre><code>package encapsuation;
  
  public class s1 {
      public  static String name;
      public static int age;
  
      public static void print()
      {
          System.out.println(name + " " + age);    
      }
  }
  </code></pre>
  
  <p>The second package contain one class</p>
  
  <pre><code>package s2;
  import encapsuation.s1;
  
  public class foo{
  
      public static void print()
      {
          System.out.println(s1.name + " " + s1.age);    
      }
  
  }
  </code></pre>
  
  <p>Is what I am trying correct?</p>
  
  <p>And why I cannot access the variables in class of the second package in the class "public class foo" but can access them in the method print only?</p>