title: Why can't I access a protected variable in Java this way?
body: <p>I've a class defined this way:</p>
  
  <pre><code>package prueba;
  
  public class OtraClase {
  
  [...]
  
  protected int num3;
  
  [...]
  </code></pre>
  
  <p>And another class defined this way:</p>
  
  <pre><code>package otro;
  
  import prueba.*;
  
  public class OtraClaseMas extends OtraClase{
  </code></pre>
  
  <p>But if in that last class I create an OtraClase object I cannot do something like this:</p>
  
  <pre><code>createdObjectOfOtraClase.num3=1;
  </code></pre>
  
  <p>And I think that according to the documentation I should be able to, <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html" rel="nofollow">here</a>. It says that the protected modifier allows for access by a subclass of its class in another package. And as much as I look at it I don't see it being another thing than exactly a subclass of its class in another package.</p>
  
  <p>Am I misunderstanding something?</p>
  
  <p>Edit: I'm either using the constructor of the class and in another different function and it doesn't work in neither place.</p>
  
  <p>Code for the constructor:</p>
  
  <pre><code>public OtraClaseMas(int num, int num2,int num3)
  {
      super(num, num2,num3);      
      OtraClase oc=new OtraClase(1,1,1);
     //oc.num3=1; This doesn't work
  }
  </code></pre>
  
  <p>Code for the method:</p>
  
  <pre><code>public void foo()
  {
      OtraClase oc=new OtraClase(1,1,1);
      //oc.num3=1; This doesn't work
  
  }
  </code></pre>