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

Misleading error message when using diamond operator with private constructor

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 8
    • 7u25, 8
    • tools
    • None
    • b108
    • Verified

    Description

      compiling code that uses the diamond operator and attempts to access a private constructor fails with a misleading error message:

      Test.java:3: error: cannot infer type arguments for MyList<>
          MyList<Object> foo = new MyList<>();
                               ^
      1 error

      The error is not that the types cannot be inferred, but that the constructor for MyList is private. If you explicitly specify the type parameter in the call to the MyList constructor, you get the correct error message telling you that the constructor is private.

      Steps to reproduce:

      I'm using javac version 1.7.0_25.

      1) Create these 2 files.

      MyList.java:
      public class MyList<E> {
        private MyList() { }
      }

      Test.java:
      public class Test {
        public void foo() {
          MyList<Object> foo = new MyList<>();
        }
      }

      2) Compile.

      $ javac Test.java
      Test.java:3: error: cannot infer type arguments for MyList<>
          MyList<Object> foo = new MyList<>();
                               ^
      1 error


      The error message says nothing about the 'real' cause of the failure - i.e. the constructor being private.

      Attachments

        Activity

          People

            vromero Vicente Arturo Romero Zaldivar
            mcimadamore Maurizio Cimadamore
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: