Consider the following two lines of code:
final List<Path> paths = new ArrayList<>();
final FileVisitor<Path> fv = new SimpleFileVisitor<>();
To me, they look quite similar. However, the second line is refused by the Java compiler (1.8) with the message "Cannot infer type arguments for SimpleFileVisitor<>".
Can anyone please explain, what's the problem?
I don't see how you may get the error message
Cannot infer type argumentsbecause your syntax is correct, except for the fact that as many have said already, the classjava.nio.file.SimpleFileVisitorhas only one constructor which isprotected:This means that only children of this class can initialize an instance of
SimpleFileVisitor, and that's why your code doesn't compile.I don't know this class, but by a quick look at the code I guess they simply expect you to extend it first (or use an already existing extension coming from somewhere else), and then use it the implementations of the
FileVisitorinterface.If you don't have a concrete child class to use and want to create your own
MySimpleFileVisitor:... you will then be able to instantiate your class and use the already implemented methods like this: