假设我有以下定义单个static
实用程序方法的类:
import java.io.IOException;
import java.nio.channels.AsynchronousSocketChannel;
public class Utility {
public static AsynchronousSocketChannel getChannel() {
try {
return AsynchronousSocketChannel.open();
} catch (IOException e) {
throw new IllegalStateException();
}
}
}
然后,我可以创建一个使用此方法的类(位于与 相同的包中Utility
):
public class Test {
public static void main(String[] args) throws Exception {
var channel = Utility.getChannel();
System.out.println(channel);
channel.close();
}
}
但是,Test
似乎不需要任何导入语句,即使它AsynchronousSocketChannel
在本地使用。如果我改为键入AsynchronousSocketChannel channel = ...;
,那么显然需要导入语句。
我的假设是在编译时(利用本地类型推断时)推断出import 语句是否正确?