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

Compiler error concerning final variables

XMLWordPrintable

    • b123
    • x86
    • windows_vista
    • Verified

      FULL PRODUCT VERSION :
      java version "1.6.0_22"
      Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
      Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Vista 32-bit:
      Microsoft Windows [Version 6.0.6001]

      A DESCRIPTION OF THE PROBLEM :
      The following code causes a compiler error.

      package test;

      import javax.swing.DefaultListModel;
      import javax.swing.JList;
      import javax.swing.JPanel;

      public class Test extends JPanel
      {
      private DefaultListModel m_listModel;
      private JList m_list;

      public void onUp()
      {
      final Object[] l_arItems;
      final int l_iMinIndex;

      l_arItems = m_list.getSelectedValues();

      for (int i = 0, l_iLen = l_arItems.length; i < l_iLen; i++)
      {
      final Object l_selected = l_arItems[i];
      final int l_iIndex = m_listModel.indexOf(l_selected);

      m_listModel.remove(l_iIndex);

      if (l_iIndex == 0 && i == 0)
      {
      m_listModel.addElement(l_selected);
      break;
      }
      else
      {
      m_listModel.add(l_iIndex - 1, l_selected);
      }
      }

      for (final Object l_selected : l_arItems)
      {
      final int l_iIndex = m_listModel.indexOf(l_selected);
      m_list.addSelectionInterval(l_iIndex, l_iIndex);
      }

      l_iMinIndex = m_list.getSelectedIndex();

      if (l_iMinIndex > -1)
      m_list.scrollRectToVisible(m_list.getCellBounds(l_iMinIndex, l_iMinIndex));
      }
      }

      Error:
      test\Test.java:56: variable l_iMinIndex might already have been assigned
                      l_iMinIndex = m_list.getSelectedIndex();
                      ^
      1 error

      Compilation works if l_iMinIndex is not final or if the lines in the second for-each-loop are removed (even though the loop does not change the value of l_iMinIndex).


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. javac test\Test.java

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The final variable is not being assigned so compilation should not result in an error.
      ACTUAL -
      Compiler error:

      "test\Test.java:56: variable l_iMinIndex might already have been assigned
                      l_iMinIndex = m_list.getSelectedIndex();
                      ^
      1 error"

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      test\Test.java:56: variable l_iMinIndex might already have been assigned
                      l_iMinIndex = m_list.getSelectedIndex();
                      ^
      1 error

      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      Remove the keyword "final".

            mcimadamore Maurizio Cimadamore
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: