title: Different behaviour of same statement while executing on JShell
body: <p>I was working on a problem to store reference of two classes within each other</p>
  
  <p>For Example:</p>
  
  <pre><code>class A {
  B b;
  A(B b){
  this.b = b;}
  }
  
  class B {
  A a;
  B(A a){
  this.a = a;}
  }
  
  public static void main(String...s){
  A a = new A(new B(null));
  a.b.a = a;
  }
  </code></pre>
  
  <p>Now if instead of above initialisation, if I use below statement:</p>
  
  <pre><code>A a = new A(new B(a));
  </code></pre>
  
  <p>I got below error which is quite obvious:</p>
  
  <pre><code>Main.java:19: error: variable a might not have been initialised
          A a = new A(new B(a));
  </code></pre>
  
  <p>But if I try the same on JShell, it works just fine (Just to be extra sure that <code>variable a</code> has never been initialized, I checked for <code>variable a</code> just before executing the statement which confirms that it was not initialized before:</p>
  
  <p><a href="https://i.stack.imgur.com/nVYDQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nVYDQ.png" alt="enter image description here"></a></p>
  
  <p>May be I am missing something here, but can some one please help me to understand why there are two different behaviours of the same statement being executed in JAVA.</p>
  
  <p>A simple way to understand this problem is that below statement is allowed in <code>Jshell</code> but not in normal program:</p>
  
  <pre><code>var somevar = somevar;
  </code></pre>