Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8200567

JShell: variable declaration allows reference in initializer

XMLWordPrintable

    • generic
    • generic

      A simpler version of the problem:

         jshell> int x = x + 3
         x ==> 3

      Which should give an error.

      ------------

      FULL PRODUCT VERSION :
      java version "10" 2018-03-20
      Java(TM) SE Runtime Environment 18.3 (build 10+46)
      Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      macOS High Sierra version 10.13.1

      A DESCRIPTION OF THE PROBLEM :

      0
      down vote
      favorite
      I was working on a problem to store reference of two classes within each other For Example:

      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;
      }
      Now if instead of above initialisation, if I use below statement:

      A a = new A(new B(a));
      I got below error which is quite obvious:

      Main.java:19: error: variable a might not have been initialised
              A a = new A(new B(a));

      But if I try the same on JShell, it works just fine (Just to be extra sure that variable a has never been initialized, I checked for variable a just before executing the statement which confirms that it was not initialized before:

      jshell> a
      | Error:
      | cannot find symbol
      | symbol: variable a
      | a
      | ^

      jshell> A a = new A(new B(a));
      a ==> A@57fffcd7

      jshell> a.b.a = a
      $4 ==> A@57fffcd7

      jshell> a.b.a
      $5 ==> A@57fffcd7

      jshell> a
      a ==> A@57fffcd7

       There are two different behaviours of the same statement being executed in JAVA using standard compiling process and JShell





      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Step 1: Start JShell
      Step 2: Execute below code:
      jshell> class A {
         ...> B b;
         ...> A(B b) {
         ...> this.b =b;
         ...> }
         ...> }
      | created class A, however, it cannot be referenced until class B is declared

      jshell> class B {
         ...> A a;
         ...> B(A a){
         ...> this.a = a;
         ...> }
         ...> }
      | created class B

      jshell> A a = new A(new B(a));
      a ==> A@57fffcd7


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      error: variable a might not have been initialised
              A a = new A(new B(a));
      ACTUAL -
      Instance created

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      class A {
      B b;
      A(B b){
      this.b = b;}
      }

      class B {
      A a;
      B(A a){
      this.a = a;}
      }

      A a = new A(new B(a));
      ---------- END SOURCE ----------

            rfield Robert Field (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: