title: Creating instance of inner class outside the outer class in java
body: <p>I'm new to Java.</p>
  
  <p>My file <code>A.java</code> looks like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class A {
      public class B {
          int k;
          public B(int a) { k=a; }
      }
      B sth;
      public A(B b) { sth = b; }
  }
  </code></pre>
  
  <p>In another java file I'm trying to create the A object calling</p>
  
  <pre class="lang-java prettyprint-override"><code>anotherMethod(new A(new A.B(5)));
  </code></pre>
  
  <p>but for some reason I get error: <code>No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A (e.g. x.new B() where x is an instance of A).</code></p>
  
  <p>Can someone explain how can I do what I want to do? I mean, do I really nead to create instance of <code>A</code>, then set it's <code>sth</code> and then give the instance of <code>A</code> to the method, or is there another way to do this?</p>