title: Accessing a private variable through static method in java
body: <p>Let say I have the following java classes:<br>
  Class A:</p>
  
  <pre><code>public class A { 
      private int x; 
  
      public A(int x){ 
          this.x = x; 
      } 
  
      public static void main(String[] args) { 
          A a = new A(1); 
          B b = new B(1,2); 
          System.out.println((A)b.x);
      }
  }
  </code></pre>
  
  <p>Class B:  </p>
  
  <pre><code>public class B extends A { 
      public int y; 
  
      public B(int x, int y){ 
          super(x); 
          this.y = y; 
      } 
  }
  </code></pre>
  
  <p>Why does the compiler marks the access to x on this line  </p>
  
  <pre><code>System.out.println((A)b.x);
  </code></pre>
  
  <p>as an error, even though I'm trying to access x from the class in which it is defined? </p>
  
  <p>Is it because of:<br>
  1. the use of polymorphism?<br>
  2. the use of a static method?<br>
  3. the use of the main method?</p>