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

Static variable initialization bug in static code block

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      A DESCRIPTION OF THE PROBLEM :
      As shown in the sample code, if A.VAR_A is initialized using a method defined in class B, it will be read as null in the static code block in class B. However, if you swap the println statements in the main method, the result is okay.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the sample Java code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A
      [A, B]
      ACTUAL -
      A
      [null, B]

      ---------- BEGIN SOURCE ----------
      import java.util.*;

      public class Main
      {
        public static void main (String[]args)
        {
          System.out.println (A.VAR_A);
          System.out.println (B.list);
        }
      }

      class A
      {
        public static final String VAR_A = B.id ("A");
        public static final String VAR_B = "B";
      }

      class B
      {
        public static final List < String > list = new ArrayList <> ();
        static
        {
          list.add (A.VAR_A);
          list.add (A.VAR_B);
        }

        public static String id (String s)
        {
          return s;
        }
      }
      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            Assignee:
            abuckley Alex Buckley
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: