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

Wrong, confusing error when non-static varargs referenced in static context

XMLWordPrintable

    • b31
    • x86
    • windows_8
    • Verified

        FULL PRODUCT VERSION :
        1.8.0_05

        ADDITIONAL OS VERSION INFORMATION :
        Windows 8.1 Update 1 (6.3.9600)

        A DESCRIPTION OF THE PROBLEM :
        When passing an array to a non-static method that takes in varargs of the same type from a static context, the error message says something like:

        method methodName in class Clazz cannot be applied to given types;
          required: Object[]
          found: Object[]
          reason: varargs mismatch; Object[] cannot be converted to Object

        This can impede progress because it does not accurately describe the issue at hand

        REGRESSION. Last worked in version 6u45

        ADDITIONAL REGRESSION INFORMATION:
        1.8.0_05, 1.8.0_11 both produce this bug. Higher versions not tested, some lower versions tested and do not produce this.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        https://stackoverflow.com/revisions/25394998/1

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Something similar to the following error message is given:

        non-static method methodName(Object...) cannot be referenced from a static context
        ACTUAL -
        Something similar to the following error message is given:

        method methodName in class Clazz cannot be applied to given types;
          required: Object[]
          found: Object[]
          reason: varargs mismatch; Object[] cannot be converted to Object

        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        method methodName in class Clazz cannot be applied to given types;
          required: Object[]
          found: Object[]
          reason: varargs mismatch; Object[] cannot be converted to Object

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        public class PrimitiveVarArgs
        {
            public static void main(String[] args)
            {
                int[] ints = new int[]{1, 2, 3, 4, 5};
                prints(ints);
            }

            void prints(int... ints)
            {
                for(int i : ints)
                    System.out.println(i);
            }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        add "static" to the non-static method and pretend the error never happened

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

                Created:
                Updated:
                Resolved: