关于你这个问题好像是编程最基本最基本的问题了吧! 在一个类中, 1,局部变量是指在语句块{}中或某个方法中定义的变量. 2,局部变量的作用于语句块或方法内部. 如: { int i; //该变量所在的语句块能访问其本身,但不能访问变量j和y ... { int j;//该变量所在的语句块能访问其本身和变量i,但不能访问变量y ... { int y;//该变量所在的语句块能访问其本身和变量i,j ... } } }
在举例一个:接分! 如: { int i; //该变量所在的语句块能访问其本身,但不能访问变量j和y ... { int j;//该变量所在的语句块能访问其本身和变量i,但不能访问变量y //int i;//这个定义会编译不过 ... { int y;//该变量所在的语句块能访问其本身和变量i,j } { int y;可以编译的过 } } }
举个例子 public class Test { public static void main(String args[]) { int sum=0; for(int i=0;i<10;i++) sum++; System.out.println(i);//不能编译,因为i是局部变量,范围只在for语句中 System.out.println(sum); } }
在一个类中,
1,局部变量是指在语句块{}中或某个方法中定义的变量.
2,局部变量的作用于语句块或方法内部.
如:
{
int i; //该变量所在的语句块能访问其本身,但不能访问变量j和y
...
{
int j;//该变量所在的语句块能访问其本身和变量i,但不能访问变量y
...
{
int y;//该变量所在的语句块能访问其本身和变量i,j
...
}
}
}
如:
{
int i; //该变量所在的语句块能访问其本身,但不能访问变量j和y
...
{
int j;//该变量所在的语句块能访问其本身和变量i,但不能访问变量y
//int i;//这个定义会编译不过
...
{
int y;//该变量所在的语句块能访问其本身和变量i,j
}
{ int y;可以编译的过
}
}
}
public class Test
{
public static void main(String args[])
{
int sum=0;
for(int i=0;i<10;i++)
sum++;
System.out.println(i);//不能编译,因为i是局部变量,范围只在for语句中
System.out.println(sum);
}
}