Confusing error message for inaccessible constructor

XMLWordPrintable

    • Type: Bug
    • Resolution: Fixed
    • Priority: P4
    • 16
    • Affects Version/s: 11.0.1, 15
    • Component/s: tools
    • b27

      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

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

              Created:
              Updated:
              Resolved: