变量的Java作用域

变量的Java作用域,java,scope,Java,Scope,我不明白为什么此代码的输出是10: package uno; public class A { int x = 10; A(){int x = 12; new B();} public static void main(String args[]){ int x = 11; new A(); } class B{ B(){System.out.println(x);} } } 本例中的范围是如何工

我不明白为什么此代码的输出是
10

package uno;

public class A
{
    int x = 10;
    A(){int x = 12; new B();}
    public static void main(String args[]){
        int x = 11;
        new A();
    }
    class B{
        B(){System.out.println(x);}
    }
}
本例中的范围是如何工作的?为什么
System.out.println(x)打印10?是因为指令
System.out.println(x)
在构造函数的父代之外:
A(){intx=12;new B();}
因此
intx=12
仅在那里存在,但在
System.out.println(x)时存在时,
x=12
不再存在??那么第一个
x
x=10
在类
A
中声明的?如果班级
A
中有
x
怎么办?它是否会打印
11

局部变量只能从声明的方法中访问。考虑到这一点,可以重写代码,以避免产生混淆:

package uno;
public class A{
  // And instance member variable (aka field)
  int field_A_x = 10;
  A(){
    // A local variable, only visible from within this method
    int constructor_x = 12;
    new B(); // -> prints 10
    // Assign a new value to field (not local variable), and try again
    field_A_x = 12;
    new B(); // -> prints 12
  }
  public static void main(String args[]){
    // A local variable, only visible from within this method
    int main_x = 11;
    new A();
  }
  class B{
    B(){
      // The inner class has access to the member variables from
      // the parent class; but the field not related to any local variable!
      System.out.println(field_A_x);
    }
  }
}
(隐藏的成员变量始终可以在
this.x
符号中访问;但我建议不要隐藏变量-选择有意义的名称。)

局部变量只能从声明它们的方法中访问。考虑到这一点,可以重写代码,以避免产生混淆:

package uno;
public class A{
  // And instance member variable (aka field)
  int field_A_x = 10;
  A(){
    // A local variable, only visible from within this method
    int constructor_x = 12;
    new B(); // -> prints 10
    // Assign a new value to field (not local variable), and try again
    field_A_x = 12;
    new B(); // -> prints 12
  }
  public static void main(String args[]){
    // A local variable, only visible from within this method
    int main_x = 11;
    new A();
  }
  class B{
    B(){
      // The inner class has access to the member variables from
      // the parent class; but the field not related to any local variable!
      System.out.println(field_A_x);
    }
  }
}

(隐藏的成员变量始终可以在
this.x
符号中访问;但我建议不要隐藏变量-选择有意义的名称。)

尝试在第四行中省略
int
时会发生什么:

package uno;
public class A{
  int x = 10;
  A(){x=12; new B();}
  public static void main(String args[]){
    int x = 11;
    new A();
  }
  class B{
    B(){System.out.println(x);}
  }
}

然后输出将为12,因为您没有初始化新的
int x

请尝试在第4行中省略
int
会发生什么:

package uno;
public class A{
  int x = 10;
  A(){x=12; new B();}
  public static void main(String args[]){
    int x = 11;
    new A();
  }
  class B{
    B(){System.out.println(x);}
  }
}
int x = 10;
然后输出将是12,因为您没有初始化新的
intx

int x = 10;
这具有实例范围,任何A类成员都可以“看到”这一点。B班看到了这一点

int x=12;
这具有本地作用域,在构造函数中。它只能在A的构造函数中看到

int x = 11; 
还有局部范围,这次是在主方法内部

谁做
System.out.println(x)?B的构造器。B看到了什么<代码>整数x=10。
这就是为什么

此外

public class A{
  int x = 10;
  A(){int x=12; new B(); System.out.println(x); //prints 12}
  public static void main(String args[]){
    int x = 11;
    System.out.println(x); // prints 11
    new A();
  }
  class B{
    B(){System.out.println(x); //prints 10, as in your example}
  }
}
这具有实例范围,任何A类成员都可以“看到”这一点。B班看到了这一点

int x=12;
这具有本地作用域,在构造函数中。它只能在A的构造函数中看到

int x = 11; 
还有局部范围,这次是在主方法内部

谁做
System.out.println(x)?B的构造器。B看到了什么<代码>整数x=10。
这就是为什么

此外

public class A{
  int x = 10;
  A(){int x=12; new B(); System.out.println(x); //prints 12}
  public static void main(String args[]){
    int x = 11;
    System.out.println(x); // prints 11
    new A();
  }
  class B{
    B(){System.out.println(x); //prints 10, as in your example}
  }
}

内部类看不到在类的构造函数中定义的局部变量。这就是为什么x代表B是10


编辑:Ninja'd

您的内部类无法看到您在类的构造函数中定义的局部变量。这就是为什么x代表B是10


编辑:Ninja'd

如果您希望输出为
x=12
,只需更改

A(){int x=12; new B();}  // remove int here

它当前正在创建一个
局部变量
,正如在方法中声明的那样

删除构造函数中的新声明将更改实例变量的值


因此,输出将是
x=12

如果您希望输出为
x=12
,只需更改即可

A(){int x=12; new B();}  // remove int here

它当前正在创建一个
局部变量
,正如在方法中声明的那样

删除构造函数中的新声明将更改实例变量的值


因此,输出将是
x=12

发生的是变量阴影。检查


如果在本地方法中声明并初始化了具有相同名称的变量,则该值仅在其定义的方法中使用。它不会更改全局变量。因此设置
intx=11
intx=12
不会更改x的全局值。然而,在方法中,如果未定义变量,则使用全局值,因此在类
B中打印x i的全局值。

发生的是变量阴影。检查


如果在本地方法中声明并初始化了具有相同名称的变量,则该值仅在其定义的方法中使用。它不会更改全局变量。因此设置
intx=11
intx=12
不会更改x的全局值。但是,在方法中,如果未定义变量,则在类
B中使用全局值
打印x i的全局值。

如果类a中有任何x怎么办?
x
位于
A
中。您正在A()中创建另一个变量,并且不修改该字段。在A()中只尝试x=12。如果类A中有x怎么办?
x
位于
A
中。您正在A()中创建另一个变量,并且不修改该字段。在A()中只尝试x=12。它当然没有全局作用域。它的作用域是类A的实例。Java中没有全局作用域。我的错。错误的术语用法。谢谢它当然没有全球范围。它的作用域是类A的实例。Java中没有全局作用域。我的错。错误的术语用法。谢谢