[英]How the second in var will be 2, since we are accessing it outside its scope
Please explain how the second in var will be 2 since, we are accessing outside it scope. 请说明var中的第二个将如何为2,因为我们正在访问它的范围之外。 o/p - in=1 out=2 in=2
o / p-in = 1 out = 2 in = 2
class Test
{
public static void main(String args[])
{
int var = 1;
System.out.println("in="+var);
{
var = 2;
System.out.println("out="+var);
}
System.out.println("in="+var);
}
}
The scope of var
is controlled by the outer declaration. var
的范围由外部声明控制。 You only have a single var
. 您只有一个
var
。 You can't shadow var
as posted because it is a local variable. 您不能像所发布的那样遮盖
var
,因为它是局部变量。 However , if we tweak it a little bit for the example. 但是 ,如果我们对示例进行一些调整。
static int var = 1;
public static void main(String args[])
{
System.out.println("in="+var);
{
int var = 2;
System.out.println("out="+var);
}
System.out.println("in="+var);
}
Does shadow the externally declared var
. 是否遮蔽外部声明的
var
。 And it does output 它确实输出
in=1
out=2
in=1
Here you declare a variable and initialize it with value int var = 1;
在这里,您声明一个变量并使用
int var = 1;
值对其进行初始化int var = 1;
Now you change it's value var = 2;
现在更改它的值
var = 2;
, so value of var is 2. ,因此var的值为2。
System.out.println("in="+var);
will print latest value of var which is 2. 将打印var的最新值为2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.