【发布时间】:2018-10-17 14:42:06
【问题描述】:
假设我有以下定义单个 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 = ...;,那么显然需要导入语句。
我在编译时(利用本地类型推断时)推断导入语句的假设是否正确?
【问题讨论】:
标签: java import var type-inference java-10