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.
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.