【发布时间】:2010-07-21 15:10:41
【问题描述】:
给定以下代码:
public class A {
static final long tooth = 1L;
static long tooth(long tooth){
System.out.println(++tooth);
return ++tooth;
}
public static void main(String args[]){
System.out.println(tooth);
final long tooth = 2L;
new A().tooth(tooth);
System.out.println(tooth);
}
}
你能解释一下阴影的概念吗?
还有一件事,main 方法的代码中实际使用了什么tooth?
我知道这是一个非常丑陋的代码,但丑陋是 SCJP 书籍作者的标准选择。
【问题讨论】:
-
What is Shadowing? 的可能重复项(虽然是 C#,但概念相同)和 What is variable shadowing used for in a Java class?
-
@Péter Török - 有没有办法确定一个问题是否与前一个问题相似?在发布之前的相关问题部分中,我没有找到(或至少我认为是)您引用的链接。
-
众所周知,SO 搜索引擎存在缺陷。有些人实际上建议使用 google 来搜索 stackoverflow.com。但是,基于标签的搜索很有用。在这种情况下,链接的两个问题都有
shadowing标签。