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

Confusing error message for inaccessible constructor

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 16
    • 11.0.1, 15
    • 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

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

              Created:
              Updated:
              Resolved: