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

Confusing error message for inaccessible constructor

    XMLWordPrintable

Details

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

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

              Dates

                Created:
                Updated:
                Resolved: