为什么 BufferedInputStream 将字段复制到局部变量,而不是直接使用该字段
当我从 中阅读源代码时,我对它为什么写这样的代码感到困惑:java.io.BufferedInputStream.getInIfOpen()
/**
* Check to make sure that underlying input stream has not been
* nulled out due to close; if not return it;
*/
private InputStream getInIfOpen() throws IOException {
InputStream input = in;
if (input == null)
throw new IOException("Stream closed");
return input;
}
为什么它使用别名而不是直接使用字段变量,如下所示:in
/**
* Check to make sure that underlying input stream has not been
* nulled out due to close; if not return it;
*/
private InputStream getInIfOpen() throws IOException {
if (in == null)
throw new IOException("Stream closed");
return in;
}
有人能给出合理的解释吗?