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

Confusing error message for inaccessible constructor

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 11.0.1, 15
    • Fix Version/s: 16
    • Component/s: tools
    • Subcomponent:
    • Resolved In Build:
      b27

      Description

      Test file:

      ~~~
      class C {
          private C(int x) {}
          private C(String x) {}
      }

      class Test {
          C c = new C(0);
      }
      ~~~

      % -> `javahome 15`/bin/javac C.java
      C.java:7: error: incompatible types: int cannot be converted to String
          C c = new C(0);
                      ^
      Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
      1 error

      Obviously the most relevant problem is that C(int) is private, not that C(String) has an incompatible type.

      Reordering the declarations prompts the expected output:

      ~~~
      class C {
          private C(String x) {}
          private C(int x) {}
      }

      class Test {
          C c = new C(0);
      }
      ~~~

      % -> `javahome 15`/bin/javac C.java
      C.java:7: error: C(int) has private access in C
          C c = new C(0);
                ^
      1 error

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              mcimadamore Maurizio Cimadamore
              Reporter:
              dlsmith Dan Smith
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: