首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么BufferedInputStream要将字段复制到局部变量而不是直接使用该字段

为什么BufferedInputStream要将字段复制到局部变量而不是直接使用该字段
EN

Stack Overflow用户
提问于 2016-03-26 10:42:04
回答 3查看 2.5K关注 0票数 106

当我阅读java.io.BufferedInputStream.getInIfOpen()的源代码时,我很困惑为什么它会写成这样的代码:

代码语言:javascript
复制
/**
 * 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,如下所示:

代码语言:javascript
复制
/**
 * 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;
}

有人能给出一个合理的解释吗?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36231047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档